有下面一段代码,包括了指针数组,二级指针,数组的应用:
1 #include <stdio.h> 2 3 int main() { 4 5 int i[] = {10, 20, 30, 40, 50}; 6 int *pa[] = {i, i+2, i+1, i+4, i+3}; 7 int **p = pa; 8 9 printf("Initial **p = %d\n", **p); 10 p++; 11 printf("After p++, the **p = %d\n", **p); 12 ++*p; 13 printf("After ++*p, the **p = %d\n", **p); 14 **p++; 15 printf("After **p++, the **p = %d\n", **p); 16 ++**p; 17 printf("After ++**p, the **p = %d\n", **p); 18 return 0; 19 }
编译执行之后,结果如下:
1 Initial **p = 10 2 After p++, the **p = 30 3 After ++*p, the **p = 40 4 After **p++, the **p = 20 5 After ++**p, the **p = 21
这个结果分析如下:
p;//指向的是pa的第一个位置 p++;//执行之后,指向pa+1的位置,也就是i+2 ++*p;//c语言结合优先顺序等于++(*p),p指向pa+1,*p指向i+2,那么执行之后,*p指向i+3 **p++;//**(p++),p指向pa+1的位置,执行之后变为了pa+2 ++**p;//这个不用说了
总结:
1. 指针很灵活
2. 结合优先级很重要