#include <stdio.h>
void changePtr(int* ptr) {
printf("Value of ptr is %d, address is %d \n" , *ptr,&ptr);
int b = 50;
ptr = &b;
printf("Value of ptr in function changePtr is %d, address is %d \n", *ptr , &ptr);
}
int main() {
int a = 10;
int* ptr;
ptr = &a;
changePtr(ptr);
printf("Value of ptr in main is %d, address is %d \n", *ptr,&ptr);
return 0;
}
Value of ptr is 10, address is 13368768
Value of ptr in function changePtr is 50, address is 13368768
Value of ptr in main is 10, address is 13368980
在这里,当我打电话功能changePtr与PTR,因为它的参数,函数changePtr没有收到指针本身,而PTR的主要价值() ,函数changePtr中的变量ptr是一个全新的指针,它复制从main()接收到的指针ptr的值。
如果C支持通过引用调用,则较低两行的输出将匹配。
C没有参考文献。 – 2501
如果你想使用['printf'](http://en.cppreference.com/w/c/io/fprintf)打印一个指针,你应该使用''%p''格式打印一个' void *'(铸造是必要的)。 –