【发布时间】:2015-02-03 08:58:04
【问题描述】:
char * read_command()
{
char command[25];
char *input = malloc(sizeof(char) * 25);
printf("myRolodex Command: ");
scanf("%c", &command);
strcpy(input, command);
return input;
}
void evaluate_command(char command[250])
{
if (strcmp(command == 'I')==0)
{
printf("\nenterned i");
}
}
int main()
{
evaluate_command(read_command))
return 0;
}
我试过这样做,但编译时得到:
rolodex.c: In function ‘read_command’:
rolodex.c:25: warning: format ‘%c’ expects type ‘char *’, but argument 2 has type ‘char (*)[25]’
rolodex.c: In function ‘evaluate_command’:
rolodex.c:32: warning: comparison between pointer and integer
rolodex.c:32: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast
/usr/include/string.h:143: note: expected ‘const char *’ but argument is of type ‘int’
rolodex.c:32: error: too few arguments to function ‘strcmp’
rolodex.c: In function ‘main’:
rolodex.c:43: warning: passing argument 1 of ‘evaluate_command’ from incompatible pointer type
rolodex.c:30: note: expected ‘char *’ but argument is of type ‘char * (*)()’
rolodex.c:43: error: expected ‘;’ before ‘)’ token
rolodex.c:43: error: expected statement before ‘)’ token
我应该让“read_command 可以打印一个提示,然后从用户那里读取一个字符。它可能希望在返回之前确保该命令是一个有效的命令。
evaluate_command 可以接受一个命令字符并决定它是哪个命令,然后执行相应的操作。”
【问题讨论】:
-
我认为编译器错误很清楚。你有什么不明白的?
-
我不太了解编译器错误。抱歉,对 c 很陌生。我的 read_command 是否复制用户输入的内容并将其放入输入中?
-
否,在您修复编译器错误之前不会。您不能只是忽略它们并希望程序仍然正确。
-
好的。请问,“格式‘%c’需要类型‘char ’,但参数2的类型是‘char ()[25]”,这是什么意思?在该行中,我需要使用其他东西而不是 %c 吗?我以为是 c 因为它是一个字符
-
查看我的答案(并放松,因为这指出了进一步的问题)
标签: c pointers return character strcpy