C pointer to a pointer
The pointer is a pointer to a multi-stage form of indirect addressing, or is a pointer chain. Typically, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, the second pointer contains the location of the actual value.
A pointer to a pointer variable must be declared as follows, namely placing two asterisks in front of the variable name. For example, the following declares a pointer to an int pointer pointers:
int **var;
When a target is a pointer to another pointer indirectly points to access this value requires the use of two asterisks operator, as shown in the following examples:
#include <stdio.h> int main () { int var; int *ptr; int **pptr; var = 3000; /* 获取 var 的地址 */ ptr = &var; /* 使用运算符 & 获取 ptr 的地址 */ pptr = &ptr; /* 使用 pptr 获取值 */ printf("Value of var = %d\n", var ); printf("Value available at *ptr = %d\n", *ptr ); printf("Value available at **pptr = %d\n", **pptr); return 0; }
When the above code is compiled and executed, it produces the following results:
Value of var = 3000 Value available at *ptr = 3000 Value available at **pptr = 3000