C Programming
In this tutorial we will learn to chain pointer variables in C programming language.
In the previous tutorial Pointers and Variables we learned how to manipulate data stored in a variable using pointer variable.
Now we will be chaining some more pointer variables to manipulate other variables.
Note! Complexity increases as we chain more pointers.
We will start by creating an integer variable num and assign value 10 to it.
num
int num = 10;
So, three things happens on writing the above code.
In the above image it is shown that the integer variable num gets the memory location 8280 and in that location the value 10 is stored.
Now we will create an integer pointer variable ptr that will point at the integer variable num.
ptr
For this we have to save the address of the num variable in the ptr variable.
To get the address of a variable we use the address of & operator.
&
// integer variable num int num = 10; // pointer variable pointing at num int *ptr = #
In the above image it is shown that the integer pointer variable ptr gets the memory location 8272 and in that location the address of the num variable i.e., 8280 is stored. So, ptr variable is pointing at the num variable.
To create a second pointer variable to point at the first pointer variable we use the following syntax.
dataType **secondPtr = &firstPtr;
In the following example we are creating the second integer pointer variable ptr2 that points at the first integer pointer variable ptr which is pointing at the integer variable num.
ptr2
int **ptr2 = &ptr;
In the above image it is shown that the second integer pointer variable ptr2 gets the memory location 8264 and in that location the address of the first pointer variable ptr i.e., 8272 is stored. So, ptr2 variable is pointing at the ptr variable.
For this we have to first get the address of the variable num which is stored in integer pointer variable ptr.
We use the value at the address of * operator to get the value stored at a given memory address.
*
To get the address of the integer variable num which is stored as a value in integer pointer variable ptr we have to write the following.
*ptr
And to get the address of the integer pointer variable ptr which is stored as a value in integer pointer variable ptr2 we have to write the following.
*(*ptr2)
So, *ptr2 is pointing at ptr and *ptr is pointing at num.
*ptr2
And *(*ptr2) is giving us the value stored at the memory address (in this case 8280) that is allocated to the num variable.
So, to update the value stored in the integer variable num via the second integer pointer variable ptr2 we have to write the following.
// updating the value of num via ptr2 *(*ptr2) = 20;
#include <stdio.h> int main(void) { // num variable int num = 10; // ptr pointer variable int *ptr = NULL; // second ptr2 pointer variable int **ptr2 = NULL; // assigning the address of num to ptr ptr = # // assigning the address of ptr to ptr2 ptr2 = &ptr; // printing the value of num - Output: 10 printf("num: %d\n", num); printf("num via ptr: %d\n", *ptr); printf("num via ptr2: %d\n", *(*ptr2)); // updating the value of num via ptr2 printf("Updating value of num via ptr2...\n"); *(*ptr2) = 20; // printing the new value of num - Output: 20 printf("num: %d\n", num); printf("num via ptr: %d\n", *ptr); printf("num via ptr2: %d\n", *(*ptr2)); return 0; }
Output:
num: 10 num via ptr: 10 num via ptr2: 10 Updating value of num via ptr2... num: 20 num via ptr: 20 num via ptr2: 20