C Pointer to pointer

C Pointers

A pointer to a pointer is a form of multiple indirection, or a chain of pointers. Normally, 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, and the second pointer points to the location that contains the actual value.

C 中指向指针的指针

A pointer to a pointer variable must be declared as follows, placing two asterisks before the variable name. For example, the following declares a pointer to an int pointer:

int **var;

When a target value is indirectly pointed to by a pointer to another pointer, accessing this value requires the use of two asterisk operators, as shown in the following example:

Example

#include <stdio.h> int main () { int V; int *Pt1; int **Pt2; V = 100; /*Get the address of V*/ Pt1 = &V; /*Use the & operator to get the address of Pt1*/ Pt2 = &Pt1; /*Use pptr to get the value*/ printf("var = %d\n", V ); printf("Pt1 = %p\n", Pt1 ); printf("*Pt1 = %d\n", *Pt1 ); printf("Pt2 = %p\n", Pt2 ); printf("**Pt2 = %d\n", **Pt2); return 0; }

When the above code is compiled and executed, it produces the following results:

var = 100
Pt1 = 0x7ffee2d5e8d8
*Pt1 = 100
Pt2 = 0x7ffee2d5e8d0
**Pt2 = 100

C Pointers

other extensions