本帖最后由 ba21 于 2017-6-19 21:04 编辑
不知道你为啥要搞个p[1],希望你看了下面能明白
直接 q=&b; 不就好了
--------------------------------------------------------------------------------------------------------
int *p,*q;
int a = 2;
int b = 3;
printf("&p=%p, &q=%p, &a=%p, &b=%p, &p[0]=%p, &p[1]=%p\n", &p, &q, &a, &b, &p[0], &p[1]);
输出:
&p=0018FF44, &q=0018FF40, &a=0018FF3C, &b=0018FF38, &p[0]=CCCCCCCC, &p[1]=CCCCCCD0
这里地址的分配是从高到底,变量的地址是系统自行分配的; &p[0]=CCCCCCCC, &p[1]=CCCCCCD0 得到的是随机值
-------------------------------------------------------------------------------------------------------------------------------------
int *p,*q;
int a = 2;
int b = 3;
p = &a;
printf("&p=%p, &q=%p, &a=%p, &b=%p, &p[0]=%p, &p[1]=%p\n", &p, &q, &a, &b, &p[0], &p[1]);
printf("p=%p, q=%p, &a=%p, &b=%p, &p[0]=%p, &p[1]=%p\n", p, q, &a, &b, &p[0], &p[1]);
输出:
&p=0018FF44, &q=0018FF40, &a=0018FF3C, &b=0018FF38, &p[0]=0018FF3C, &p[1]=0018FF40
p=0018FF3C, q=CCCCCCCC, &a=0018FF3C, &b=0018FF38, &p[0]=0018FF3C, &p[1]=0018FF40
--------------------------------------------------------------------------------------------------------------------------------------------
int *p,*q;
int a = 2;
int b = 3;
p = &a;
//方法一------
q=&p[1];
printf("&p=%p, &q=%p, &a=%p, &b=%p, &p[0]=%p, &p[1]=%p\n", &p, &q, &a, &b, &p[0], &p[1]);
printf("p=%p, q=%p, &a=%p, &b=%p, &p[0]=%p, &p[1]=%p\n", p, q, &a, &b, &p[0], &p[1]);
输出
&p=0018FF44, &q=0018FF40, &a=0018FF3C, &b=0018FF38, &p[0]=0018FF3C, &p[1]=0018FF40
p=0018FF3C, q=0018FF40, &a=0018FF3C, &b=0018FF38, &p[0]=0018FF3C, &p[1]=0018FF40
---------------------------------------------------------------------------------------------------------------------------------------------
int *p,*q;
int a = 2;
int b = 3;
p = &a;
//方法一------
q=&p[1];
*q = b;
printf("&p=%p, &q=%p, &a=%p, &b=%p, &p[0]=%p, &p[1]=%p\n", &p, &q, &a, &b, &p[0], &p[1]);
printf("p=%p, q=%p, &a=%p, &b=%p, &p[0]=%p, &p[1]=%p\n", p, q, &a, &b, &p[0], &p[1]);
输出
&p=0018FF44, &q=0018FF40, &a=0018FF3C, &b=0018FF38, &p[0]=0018FF3C, &p[1]=0018FF40
p=0018FF3C, q=00000003, &a=0018FF3C, &b=0018FF38, &p[0]=0018FF3C, &p[1]=0018FF40
走到这里不难看出吧。。搞了半了就是把指针q里面存放的值赋值为3;最后在用数组的形式p[1]来索引该处的值。