Pointer pointers

Pointer pointers are pointers that point to pointers. We’re really down the rabbit hole, folks.

A pointer pointer is a memory location that holds the address of a pointer. We use pointer pointers when attempting to manipulate another pointer by reference, essentially. We mostly encounter pointer pointers in the wild when dealing with dynamically allocated data structures that need to be passed ‘by value’ to a function.

A pointer pointer will hold the address of a pointer, which in turn holds the address of the block of data we want to work with. Pointer pointers are can also be called double indirection.

Pointer pointers aren’t as difficult as they sound, but they’re as far out as I personally am willing to get.

int main() {

	int iNum = 42;
	int *iNumPtr = NULL;
	int **iNumPtrPtr = NULL;

	printf("The address of the actual data is %p and it holds %d\n", &iNum, iNum);
	
	//assign address of iNum to iNumPtr
	iNumPtr = &iNum;

	printf("The address of the pointer to the data is %p and it holds %p\n", &iNumPtr, iNumPtr);

	//assign address of iNumPtr to iNumPtrPtr
	iNumPtrPtr = &iNumPtr;

	printf("The adress of the pointer to the pointer to the data is %p and it holds %p\n", &iNumPtrPtr, iNumPtrPtr);


	printf("We can access the original data via the pointer pointer. It's %d!!!\n", **iNumPtrPtr);

	return 0;

}

One of the virtues of pointer pointers is that they enable us to perform the dynamic initialization of arrays and strings from within a function. We note here that the C language copies all function parameters onto the stack, thus creating a unique variable that we then use as a local variable in the function. To pass by value, we pass the function the address of the data we wish to use locally in the function, which is then copied onto the stack. So within the function, the pointer itself is a unique copy, even if the data it points to has scope outside the function.

void workingWithParameters(char *string, char **szPtr);

int main() {

	char *string = "Nice boat!";

	printf("The address of the string is %p\n", &string);
	printf("And its value is %s\n", string);

	workingWithParameters(string, &string);

	printf("Back in main() the value is now %s\n", string);

	return 0;

}


void workingWithParameters(char *string, char **szPtr) {
	printf("The address of the string on the stack is %p\n", &string);
	printf("But back in main() the string is located really at %p\n", *szPtr);
	printf("And its value is %s\n", *szPtr);
	*szPtr = "For great justice!";
}

 

Pointer pointers are typically used as function parameters. It’s important to remember that all function parameters are local to the function, even ones that are passed by reference via the pointer mechanism.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s