Reference: C pointer to pointer
Pointer is a pointer to a multi-stage form of indirect addressing, or is a chain of pointers. 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 to an actual position values.
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 declaration of a pointer pointing to a pointer of type int:
int ** were ;
When a target value is a pointer pointing to another pointer indirectly, to access the values requires two asterisks operator, as shown in the following example:
- var: int variables
- ptr: Pointer to the var
- pptr: the pointer ptr
- Var value represents three methods:
- where
- * Ptr: ptr pointer corresponding to a value, i.e., var
- ** pptr: * pptr corresponding pointer value PTR i.e., so ** pptr = * ptr = var
- Ptr represents the value (var address, pptr value corresponding to) four methods:
- & Var: var represents an address, i.e. ptr
- ptr
- & (* Ptr): * ptr corresponds var
- * Pptr: * pptr corresponding pointer value PTR i.e., so * pptr = ptr = & var
- It represents three methods pptr value (PTR address):
- & Ptr: indicates the address of ptr, that pptr
- pptr
- & (* Pptr): * pptr equivalent to ptr
- It represents pptr address ways:
- &pptr
#include <stdio.h> int main() { int var; int *ptr; int **pptr; var = 3000; ptr = &var; pptr = &ptr; printf("Value of var = %d\n", var ); printf("Value available at *ptr = %d\n", *ptr ); printf("Value available at **pptr = %d\n", **pptr); printf("\n"); printf("Address of var: &var = %p\n", &var); printf("Address of var: ptr = %p\n", ptr); printf("Address of var: &(*ptr) = %p\n", &(*ptr)); printf("Address of var: *pptr = %p\n", *pptr); printf("\n"); printf("Address of ptr: &ptr = %p\n", &ptr); printf("Address of ptr: pptr = %p\n", pptr); printf("Address of ptr: &(*pptr) = %p\n", &(*pptr)); printf("\n"); printf("Address of pptr: &pptr = %p\n", &pptr); return 0; }
Outputs:
Value of var = 3000 Value available at *ptr = 3000 Value available at **pptr = 3000 Address of var: &var = 0x7ffff21a1b64 Address of var: ptr = 0x7ffff21a1b64 Address of var: &(*ptr) = 0x7ffff21a1b64 Address of var: *pptr = 0x7ffff21a1b64 Address of ptr: &ptr = 0x7ffff21a1b68 Address of ptr: pptr = 0x7ffff21a1b68 Address of ptr: &(*pptr) = 0x7ffff21a1b68 Address of pptr: &pptr = 0x7ffff21a1b70