Skip to content
On this page

C语言二级指针


标签:clang/basic  

二级指针,本质上也是一个指针,存储的是指针变量的地址

定义格式:数据类型 **指针名;

指针变量本身也是一个变量, 它当然拥有自己的地址, 但是我们取它的地址的话就有问题:

c
int num = 520;
int *p = #
int *q = &p;

编译器马上报警了:

bash
test2.c:8:14: warning: initialization of ‘int * from incompatible pointer type ‘int ** [-Wincompatible-pointer-types]
    8 |     int *q = &p;
      |

告诉我们 &p 返回的类型是 int ** , 不是 int *, 将其赋值后, 如果调用变量, 将引起程序崩溃.

所以二级指针的声明是:

c
int **q = &p;
printf("%d\n", **p);

数据获取的转换运算符也变为 **.

Last updated: