int main()
{
char name[10], food[10], color[10], ans[10];
int height, year, age, date;
/* Ask for user inputs and do stuff here */
/* ------------------------------------- */
printf("Would you like me to repeat this?\n");
scanf("%c", ans);
/* its giving me a warning for the if condition on the line */
/* below saying warning: comparison b/w pointer and integer. */
if (ans == 'y')
printf("ok I will\n");
else
printf("fine.\n");
return 0;
}
在程序的最后,我写了一个 IF 语句,如果用户输入“y”,那么它会说“好的,我会的”,否则会说“好”——没什么特别的。 但是当我运行它时,程序会问我“你想让我重复一遍吗?”即使我输入“y”,程序也会输出“好”而不是“好的,我会”……请帮忙。
最佳答案
变量 ans
是一个数组,可以衰减为指向第一个元素的指针。你把它比作一个角色。
你应该改成例如ans[0] == 'y'
。
或者更好的是,既然你只读了一个字符,为什么还要我们一个数组呢?只需将 ans
声明为单个字符,并在 scanf
调用中使用 address-of 运算符:
char ans;
scanf(" %c", &ans);
if (tolower(ans) == 'y') ...
关于c - 警告 : comparison b/w pointer and integer in C.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32542921/