++和*对指针的混用:
优先级:++和*同级,但是从右往左运算
*(p++) 等价:*p++,指针p后++再取值,实际是对p的取值
*(++p) 等价:*++p,指针p先++后取值,实际是对p+1的取值
++(*p) 等价:++*p,对p的取值的操作,p的值先++
(*p)++ 必须括号*p,对p的取值的操作,p的值后++
实验程序:
(1)*p++:
int n[]={1,3,5},*p=n;
printf("p = %d\n",p);
printf("*p++ = %d\n",*p++);
printf("p = %d\n",p);
printf("*p = %d",*p);
p = 6487552
*p++ = 1
p = 6487556
*p = 3
(2) *++p:
int n[]={1,3,5},*p=n;
printf("p = %d\n",p);
printf("*++p = %d\n",*++p);
printf("p = %d\n",p);
printf("*p = %d",*p);
p = 6487552
*++p = 3
p = 6487556
*p = 3
(3) ++*p:
int n[]={1,3,5},*p=n;
printf("p = %d\n",p);
printf("(++*p = %d\n",++*p);
printf("p = %d\n",p);
printf("*p = %d",*p);
p = 6487552
(++*p = 2
p = 6487552
*p = 2
(4) (*p)++:
int n[]={1,3,5},*p=n;
printf("p = %d\n",p);
printf("(*p)++ = %d\n",(*p)++);
printf("p = %d\n",p);
printf("*p = %d",*p);
p = 6487552
(*p)++ = 1
p = 6487552
*p = 2
总结:
一、++p(或p++)的指针操作后取值
二、++(*p), (*p)++是对p的取值进行数值操作