【发布时间】:2013-08-03 01:15:25
【问题描述】:
我不太明白这些错误是从哪里来的。我正在尝试创建一个简单的 C 程序,它接受一个字符串并向 ASCII 值添加一个偏移量,以创建一个极其简单的加密。
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
char obscufate_char(char origchar, int offset){
if(strcmp(origchar, " ") != 0){
int temp = origchar;
char newChar = temp + 4;
return newChar;
}
else{
return 20;
}
}
int main(int argc, string argv[]){
if(argv[1] != NULL){
string message = argv[1];
}else{
printf("%s\n", "Enter a string to encrypt\n");
string message = GetString();
}
if(argv[2] != NULL){
int offset = atoi(argv[2]);
}else{
printf("%s\n", "Enter a offset\n");
int offset = GetInt();
}
printf("%s%s\n", "Your original text is: ", message);
printf("%s\n", "Your new message is: ");
for(int i = 0; i < strlen(message); i++){
printf("%c\n", obscufate_char(message[i]),offset);
}
return 0;
}
simple_crypt.c:7:12: error: incompatible integer to pointer conversion passing
'char' to parameter of type 'const char *'; take the address with &
[-Werror]
if(strcmp(origchar, " ") != 0){
^~~~~~~~
&
/usr/include/string.h:143:34: note: passing argument to parameter '__s1' here
extern int strcmp (__const char *__s1, __const char *__s2)
^
simple_crypt.c:20:10: error: unused variable 'message'
[-Werror,-Wunused-variable]
string message = argv[1];
^
simple_crypt.c:23:10: error: unused variable 'message'
[-Werror,-Wunused-variable]
string message = GetString();
^
simple_crypt.c:27:7: error: unused variable 'offset' [-Werror,-Wunused-variable]
int offset = atoi(argv[2]);
^
simple_crypt.c:30:7: error: unused variable 'offset' [-Werror,-Wunused-variable]
int offset = GetInt();
^
simple_crypt.c:33:46: error: use of undeclared identifier 'message'
printf("%s%s\n", "Your original text is: ", message);
^
simple_crypt.c:35:28: error: use of undeclared identifier 'message'
for(int i = 0; i < strlen(message); i++){
^
simple_crypt.c:36:33: error: use of undeclared identifier 'message'
printf("%c\n", obscufate_char(message[i]),offset);
【问题讨论】:
-
你的意思是你不明白为什么会出现这些错误?因为编译器会准确地告诉你它们来自哪里,而且它会告诉你它认为什么是错误的
-
我不知道错误来自哪里似乎使用了所有变量
-
两个提示:第一个错误是无效类型,所以找出“指针和引用”,其他错误是因为您使用了超出其范围的变量,请参阅“c中的变量范围规则”。当我有更多时间来提供更完整的答案时,我会尝试回来......
-
你拼错了“混淆”。