(1)加减
-
*p+1
值+1#include<stdio.h> int main(){ int i[] = {10, 20, 30, 40, 50}; int *p = i; printf("%d",*p+1); // 11 return 0; }
-
p+1
地址加一#include<stdio.h> int main(){ int i[] = {10, 20, 30, 40, 50}; int *p = i; printf("%d",p+1); // 6422020等同于&i[1] return 0; }
(2)自增自减
-
*p++
等同于*(p++)
先取值,然后指针向后移动一位#include<stdio.h> int main(){ int i[] = {10, 20, 30, 40, 50}; int *p = i; printf("%d",*p++); // 10 printf("%d",*p); // 20 return 0; }
-
(*p)++
先取值,然后值+1#include<stdio.h> int main(){ int i[] = {10, 20, 30, 40, 50}; int *p = i; printf("%d",*p++); // 10 printf("%d",*p); // 11 return 0; }
-
++*p
*p的值+1#include<stdio.h> int main(){ int i[] = {10, 20, 30, 40, 50}; int *p = i; printf("%d",++*p); // 11 printf("%d",*p); // 11 return 0; }
(3)相加相减
-
相加无意义
-
相减
指针相减并不是字节数差值,而是距离,(p1地址-p2地址)/sizeof(int)
#include<stdio.h> int main(){ int i[] = {10, 20, 30, 40, 50}; int *p1 = &i[1]; int *p2 = &i[3]; printf("%d",p1); // 6422004 printf("%d",p2); // 6422012 printf("%d",p2-p1); // 2 return 0; }