我试图向用户显示一系列问题,然后扫描答案。 我的代码构建没有错误,但是当我运行它时,我收到错误:期望指向 char 的指针,但发现指向聚合的指针。这里有什么错误?
#include <stdio.h>
int main ()
{
char name[50] , lastname[50] , add[100], post[50], town[60], state[60], tel[50];
printf ("**** PLEASE ENTER THIS CONFIDENTIAL INFORMATION****");
printf ("\n=================================================\n=================================================");
printf ("\n\nFirst name:");
scanf ("%s",&name);
printf ("\nLast name:");
scanf ("%s",&lastname);
printf ("\nAddress Please:");
scanf ("%s",&add);
printf ("\nPostcode:");
scanf("%s",&post);
printf ("\ntown:");
scanf ("%s",&town);
printf("\nTelephone number:");
scanf("%s",&tel);
printf ("\n\n****CONFIDENTIAL INFORMATION****");
printf ("\n=================================================\n=================================================");
printf ("\nName:%s %s \n" ,name ,lastname);
printf ("Address:%s\n",add);
printf ("postal code:%s\n",post);
printf ("Town:%s\n",town);
printf ("State:%s\n",state);
printf ("Tel:%s\n",tel);
}
尝试这个(&全部删除)请参阅我上面的评论以了解原因。
#include <stdio.h>
int main ()
{
char name[50] , lastname[50] , add[100], post[50], town[60], state[60], tel[50];
printf ("**** PLEASE ENTER THIS CONFIDENTIAL INFORMATION****");
printf ("\n=================================================\n=================================================");
printf ("\n\nFirst name:");
scanf ("%s",name);
printf ("\nLast name:");
scanf ("%s",lastname);
printf ("\nAddress Please:");
scanf ("%s",add);
printf ("\nPostcode:");
scanf("%s",post);
printf ("\ntown:");
scanf ("%s",town);
printf("\nTelephone number:");
scanf("%s",tel);
printf ("\n\n****CONFIDENTIAL INFORMATION****");
printf ("\n=================================================\n=================================================");
printf ("\nName:%s %s \n" ,name ,lastname);
printf ("Address:%s\n",add);
printf ("postal code:%s\n",post);
printf ("Town:%s\n",town);
printf ("State:%s\n",state);
printf ("Tel:%s\n",tel);
}
当你对字符串进行 scanf 时,你不需要
scanf("%s",&str);
而是直接做
scanf("%s",str).
#include <stdio.h>
int main()
{
int i=3;
int *j;
j = &i;
printf("i %d\n",i);//value of i
printf("j %d",*j);//value of i in j
printf("j %d",&j);// address of i in j
return 0;
}
因此,这就是引用和取消引用指针的方式。我刚刚给出了线索。现在你用你的大脑......干杯!
问题出在 scanf 上:
printf ("\n\nFirst name:");
scanf ("%s",&name);
printf ("\nLast name:");
scanf ("%s",&lastname);
printf ("\nAddress Please:");
scanf ("%s",&add);
printf ("\nPostcode:");
scanf("%s",&post);
printf ("\ntown:");
scanf ("%s",&town);
printf("\nTelephone number:");
scanf("%s",&tel);
您应该将这样的数组传递给 scanf (不带地址运算符 &):
printf ("\n\nFirst name:");
scanf ("%s",name);
printf ("\nLast name:");
scanf ("%s",lastname);
printf ("\nAddress Please:");
scanf ("%s",add);
printf ("\nPostcode:");
scanf("%s",post);
printf ("\ntown:");
scanf ("%s",town);
printf("\nTelephone number:");
scanf("%s",tel);
这是为什么呢? 因为数组名称本身是指向其中元素首地址的指针,例如:
int x[2] = {0, 1};
x 指向第一个元素的地址,在本例中为 0 并且您还可以将数组名称视为普通指针(并非所有情况都允许) 例如:
int x[2] = {0, 1};
*(x + 0) = 55;
*(x + 1) = 105;
使用gdb调试器的输出:
5 int x[2] = {0, 1};
(gdb) s
7 *(x+0) = 55;
(gdb) p x
$1 = {0, 1}
(gdb) s
8 *(x+1) = 105;
(gdb) p x
$2 = {55, 1}
(gdb) s
10 return (0);
(gdb) p x
$3 = {55, 105}
但是这是违法的
int x[2] = {0, 1};
int y = 200;
int *p = NULL;
//x = &y; wont work as x is like a const pointer only
p = &y; //will work normally and p will point to address of y
希望这有帮助