C程序语言:printf语句中的变量前加个&是什么意思?如:printf("%d",&p);

来源:百度知道 编辑:UC知道 时间:2024/07/05 02:41:10
若不加&的话,又表达什么意思呢?

&代表取地址符,该句printf的结果就是p变量的地址

1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int main()
5 {
6 int *p;
7 int n = 4;
8 p = n;
9 printf("*p is %d\n", p);
10 printf("&p is %d\n", &p);
11 }

print.c: In function ‘main’:
print.c:8: warning: assignment makes pointer from integer without a cast
print.c:9: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
print.c:10: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int **’
wailer@ubuntu:~/program/test$ ./a.out
*p is 4
&p is -1074934944

明显的问题是:&P打印的是p的地址,如果你按照我上面的程序打印&n,则他的地址比p的地址大四。

~

变量P所指向的地址

取址运算符,即指向p这个变量所对应的值。