简单C指针
指向整数的指针,以及指针的指针
[email protected]:~?%?vim?test1.c
#include?<stdio.h>
int?main(void){
????????int?x;
????????x=128;
????????int?*myp=&x;
????????int?**mypp=&myp;
????????printf("x:%d\n",x);
????????printf("myp:%u\n",myp);
????????printf("mypp:%u\n",mypp);
????????return?1;
}
上面程序定义了一个int型的整数,然后定义了2个指针,1个是myp,另1个是mypp。
myp和mypp都是指针变量,但指向的内容是不同的,myp指向x的地址,mypp指向myp的地址,通过myp可以找到x,而通过mypp则不能立即找到x,mypp先找到myp,然后再通过myp找到x,因此,mypp也称为指针的指针。
[email protected]:~?%?make
cc?test1.c?-o?mytest
[email protected]:~?%?./mytest
x:128
myp:4294957796
mypp:4294957784
[email protected]:~?%?
通过解引用操作符*提取指针指向的内容。
[email protected]:~?%?./mytest
x:128
myp:4294957796
mypp:4294957784
*myp:128
**mypp:128
代码为
#include?<stdio.h>
int?main(void){
????????int?x;
????????x=128;
????????int?*myp=&x;
????????int?**mypp=&myp;
????????printf("x:%d\n",x);
????????printf("myp:%u\n",myp);
????????printf("mypp:%u\n",mypp);
????????printf("*myp:%d\n",*myp);
????????printf("**mypp:%u\n",**mypp);
????????return?1;
}
*myp提取的是x的内容
而**mypp提取的是也是x的内容
那么*mypp提取的内容什么呢
是myp指针变量本身的内容,即x的地址。
可修改上述程序验证一下
#include?<stdio.h>
int?main(void){
????????int?x;
????????x=128;
????????int?*myp=&x;
????????int?**mypp=&myp;
????????printf("x:%d\n",x);
????????printf("myp:%u\n",myp);
????????printf("mypp:%u\n",mypp);
????????printf("*myp:%d\n",*myp);
????????printf("**mypp:%u\n",**mypp);
????????printf("*mypp:%u-myp%u\n",*mypp,myp);
????????return?1;
}
??????????????????????????????????????????????????
程序执行结果:看最后1行
[email protected]:~?%?./mytest
x:128
myp:4294957796
mypp:4294957784
*myp:128
**mypp:128
*mypp:4294957796-myp4294957796
[email protected]:~?%?
原文地址:http://blog.51cto.com/13959448/2325038
时间: 2024-11-12 20:19:44