1 # define A 10 2 int main(void) 3 { 4 int a[5] = {1, 2, 3, 4, 5}; 5 int b = 2; 6 int *p = &a[0]; 7 int *q = &b; 8 int **r = &p; 9 char *x[] = {"about","of"}; 10 11 printf("%s ", *x); 12 13 14 printf("%d ", A); 15 printf("%d ", *p); 16 printf("%d ", *q); 17 printf("%d ", **r); 18 printf("%d ", *r); 19 printf("%d ", p); 20 printf("%d ", r); 21 22 return 0; 23 }
当用一维数组通过指针的形式定义一个数组时,如果我们不通过*x[++i]的形式来使指针移动,那么只能使用指向指针的指针来将指针移动,使其能够指向下一个地址,所以在这里我们可以通过定义如下指针:char **a = x,此时*a就是一个定义为指向指针x的指针,其指向的是x[]首地址,那么要输出of只需要*++a,打印即可。
我们有定义int *y,因此实用*y,而这里定义了**y,但是依然使用的是y,这个应该是告诉编译器,接下来要使用一个指向指针的指针结构,虽然使用的是*y,但是它已经指向另一个指针了,编译器也有相关显示:
这里再补充一个知识点,如果定义了i,j作为数组参数使用我们针对二维数组还可以用*(*(y+i)+j)形式调用,知识点在二维数组介绍的地方,由于y位置是二维数组开始位置,+i、+j对应指向行列,可用来打印相应字符。