Pointer to Pointer

A pointer to a pointer is literally that, by which like any other pointer it contains the address of the item being pointed to, in this another pointer's address

 

Double asterisk ** is used to indicate level of indirection: int **myPtr2Ptr ; //declares a pointer to a pointer

 

 

#include <stdio.h>

int main() {

	//create & display an int variable
	int myVar = 1942 ;
	printf("myVar at address: %x, contains: %d \n", &myVar, myVar);

	//create & display an int pointer to the above variable
	int *myPtr ;
	myPtr = &myVar ;
	printf("*ptr at address: %x, contains %x and points to %d \n", &myPtr, myPtr, *myPtr);

	//create & display an int pointer to the above int pointer
	int **ptr2ptr ;
	ptr2ptr = &myPtr ;
	printf("**ptr2ptr at address: %x contains %x and points to %d \n", &ptr2ptr, ptr2ptr, **ptr2ptr);

	return 0;
}

 

Compile & Run:

myVar at address: 22fd6c, contains: 1942
*ptr at address: 22fd60, contains 22fd6c and points to 1942
**ptr2ptr at address: 22fd58 contains 22fd60 and points to 1942

 

 

Leave a Reply