Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Pointers to Pointers

C Pointers to Pointers

C Pointer C Pointer

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, which points to the location containing the actual value.

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



int **var;

When a target value is indirectly pointed to by one pointer through another pointer, accessing this value requires using the double asterisk operator, as shown in the example below:

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 code above is compiled and executed, it produces the following result:



var = 100

Pt1 = 0x7ffee2d5e8d8

*Pt1 = 100

Pt2 = 0x7ffee2d5e8d0

**Pt2 = 100

C Pointer C Pointer