int *p;
scanf("%d",&p);
printf("%d\n",p);在我过去的理解中,"p“是一个地址,但现在看来p是一个简单的变量。
我不明白为什么这3行代码是正确的!
你能帮帮我吗?
发布于 2013-09-23 17:20:52
他们是不对的。它们似乎是工作的,因为您要求scanf存储一个整数,存储它的地址是指针 p的地址。基本上,您将指针本身的存储视为整数的存储。同样,对于printf,您传递指针的地址(其中包含整数),并要求printf将其读取为.一个整数。您甚至可以将第一行更改为
float* p;而且它似乎仍然有效。最后,这是一个很好的例子,说明了为什么您应该避免类型不安全的C风格接口。
发布于 2013-09-23 17:25:17
这只有在指针与整数大小相同的情况下才能工作,因为基本上是将指针视为整数。也就是说,如果int是32位整数,指针void*是32位地址.
它的写法是:
int p; // not the lack of the *
scanf("%d",&p); // this gives scanf the address of p
printf("%d\n",p); // this uses p's value它将实际使用p作为整数,而不是将其声明为指针并将其视为整数。
发布于 2013-09-23 17:22:48
如果我能通过你的陈述来解释,那就是
int *p; //Declaration of pointer variable p, which can hold the address of integer variable
scanf("%d",&p); //Getting input, will be stored at address of pointer variable p
printf("%d\n",p); //It will display the value stored at &phttps://stackoverflow.com/questions/18965140
复制相似问题