问题描述
我不断收到该警告。 我正在分享我的代码,请查看并告诉指针在哪里。
#include<stdio.h>
int main() {
int ageofUser,age;
char damru[100],professionUser[20],name[20];
printf("Hello I am damroo,your personal chatbot.\n But I can answer limited questions because I am in developing phase.\n so lets chat.\n So,What is your name ?\n");
scanf("%s",&damru);
printf("Ohkay,what is your age ?");
scanf("%d",&ageofUser);
printf("Ohkay,Mr. %d,What do you work ?",&ageofUser);
scanf("%s",&professionUser);
printf("Why don't you talk bidirectionally ?");
if (scanf("%s",&name) == "What is your name ?");
printf("I am damroo.");
if (scanf("%d",&age) == "what is your age ?");
printf("I was born on 17/02/2021");
return 0;
}
我只想知道指针在哪里。我找不到。
解决方法
scanf(3)
的 documentation 说
返回值
成功时,这些函数返回成功匹配的输入项数 并分配;这可能比提供的少 在早期匹配失败的情况下,甚至为零。
因此,您不能将 scanf()
与字符串进行比较。您需要使用 strcmp()
:
char *str = "hello";
char *str_comp = "ello";
if (strcmp(str,str_comp) == 0) {
// both are equal
} else {
// otherwise,not
}
请注意,您可以直接比较字符串常量。
为了扫描字符数组,不要在 scanf()
中引入 & 符号:
scanf("%s",professionUser); // removed &
类似地,您永远不需要在 printf()
中放入 & 号。要进行防御性编码,请始终检查 scanf()
、fgets()
等的返回值。