Chain of Pointers in C with Examples
Last Updated :
18 Jan, 2022
Prerequisite: Pointers in C, Double Pointer (Pointer to Pointer) in C
A pointer is used to point to a memory location of a variable. A pointer stores the address of a variable.
Similarly, a chain of pointers is when there are multiple levels of pointers. Simplifying, a pointer points to address of a variable, double-pointer points to a variable and so on. This is called multiple indirections.
Syntax:
// level-1 pointer declaration
datatype *pointer;
// level-2 pointer declaration
datatype **pointer;
// level-3 pointer declaration
datatype ***pointer;
.
.
and so on
The level of the pointer depends on how many asterisks the pointer variable is preceded with at the time of declaration.
Declaration:
int *pointer_1;
int **pointer_2;
int ***pointer_3;
.
.
and so on
Level of pointers or say chain can go up to N level depending upon the memory size. If you want to create a pointer of level-5, you need to precede the pointer variable name by 5 asterisks(*) at the time of declaration.
Initialization:
// initializing level-1 pointer
// with address of variable 'var'
pointer_1 = &var;
// initializing level-2 pointer
// with address of level-1 pointer
pointer_2 = &pointer_1;
// initializing level-3 pointer
// with address of level-2 pointer
pointer_3 = &pointer_2;
.
.
and so on
Example 1:

As shown in the diagram, variable ‘a’ is a normal integer variable which stores integer value 10 and is at location 2006. ‘ptr1’ is a pointer variable which points to integer variable ‘a’ and stores its location i.e. 2006, as its value. Similarly ptr2 points to pointer variable ptr1 and ptr3 points at pointer variable ptr2. As every pointer is directly or indirectly pointing to the variable ‘a’, they all have the same integer value as variable ‘a’, i.e. 10
Let’s understand better with below given code:
C
#include <stdio.h>
int main()
{
int var = 10;
int * ptr1;
int ** ptr2;
int *** ptr3;
ptr1 = &var;
ptr2 = &ptr1;
ptr3 = &ptr2;
printf ("Value of variable "
"var = %d\n",
var);
printf ("Value of variable var using "
" pointer ptr1 = %d\n",
*ptr1);
printf ("Value of variable var using "
" pointer ptr2 = %d\n",
**ptr2);
printf ("Value of variable var using "
" pointer ptr3 = %d\n",
***ptr3);
return 0;
}
|
Output:
Value of variable var = 10
Value of variable var using pointer ptr1 = 10
Value of variable var using pointer ptr2 = 10
Value of variable var using pointer ptr3 = 10
Example 2: Consider below-given code where we have taken float data type of the variable, so now we have to take same data type for the chain of pointers too. As the pointer and the variable, it is pointing to should have the same data type.
C
#include <stdio.h>
int main()
{
float var = 23.564327;
float *ptr1, **ptr2, ***ptr3, ****ptr4;
ptr1 = &var;
ptr2 = &ptr1;
ptr3 = &ptr2;
ptr4 = &ptr3;
printf ("Value of var = %f\n", var);
printf ("Value of var using level-1"
" pointer = %f\n",
*ptr1);
printf ("Value of var using level-2"
" pointer = %f\n",
**ptr2);
printf ("Value of var using level-3"
" pointer = %f\n",
***ptr3);
printf ("Value of var using level-4"
" pointer = %f\n",
****ptr4);
return 0;
}
|
Output:
Value of var = 23.564327
Value of var using level-1 pointer = 23.564327
Value of var using level-2 pointer = 23.564327
Value of var using level-3 pointer = 23.564327
Value of var using level-4 pointer = 23.564327
Example 3: Updating variable using chained pointer
As we already know that a pointer points to address location of a variable so when we access the value of pointer it’ll point to the variable’s value. Now to update the value of variable, we can use any level of pointer as ultimately every pointer is directly or indirectly pointing to that variable only. It’ll directly change the value present at the address location of variable.
C
#include <stdio.h>
int main()
{
int var = 10;
int *ptr1, **ptr2, ***ptr3;
ptr1 = &var;
ptr2 = &ptr1;
ptr3 = &ptr2;
printf ("Before:\n");
printf ("Value of var = %d\n", var);
printf ("Value of var using level-1"
" pointer = %d\n",
*ptr1);
printf ("Value of var using level-2"
" pointer = %d\n",
**ptr2);
printf ("Value of var using level-3"
" pointer = %d\n",
***ptr3);
***ptr3 = 35;
printf ("After:\n");
printf ("Value of var = %d\n", var);
printf ("Value of var using level-1"
" pointer = %d\n",
*ptr1);
printf ("Value of var using level-2"
" pointer = %d\n",
**ptr2);
printf ("Value of var using level-3"
" pointer = %d\n",
***ptr3);
return 0;
}
|
Output:
Before:
Value of var = 10
Value of var using level-1 pointer = 10
Value of var using level-2 pointer = 10
Value of var using level-3 pointer = 10
After:
Value of var = 35
Value of var using level-1 pointer = 35
Value of var using level-2 pointer = 35
Value of var using level-3 pointer = 35
Note: Level-N pointer can only be used to point level-(N-1) pointer. Except for Level-1 pointer. The level-1 pointer will always point to the variable.
Similar Reads
C Pointers
A pointer is a variable that stores the memory address of another variable. Instead of holding a direct value, it has the address where the value is stored in memory. This allows us to manipulate the data stored at a specific memory location without actually using its variable. It is the backbone of
10 min read
Pointer Arithmetics in C with Examples
Pointer Arithmetic is the set of valid arithmetic operations that can be performed on pointers. The pointer variables store the memory address of another variable. It doesn't store any value. Hence, there are only a few operations that are allowed to perform on Pointers in C language. The C pointer
10 min read
Applications of Pointers in C
Pointers in C are variables that are used to store the memory address of another variable. Pointers allow us to efficiently manage the memory and hence optimize our program. In this article, we will discuss some of the major applications of pointers in C. Prerequisite: Pointers in C. C Pointers Appl
4 min read
Passing Pointers to Functions in C
Prerequisites: Pointers in CFunctions in C Passing the pointers to the function means the memory location of the variables is passed to the parameters in the function, and then the operations are performed. The function definition accepts these addresses using pointers, addresses are stored using po
2 min read
C - Pointer to Pointer (Double Pointer)
In C, double pointers are those pointers which stores the address of another pointer. The first pointer is used to store the address of the variable, and the second pointer is used to store the address of the first pointer. That is why they are also known as a pointer to pointer. Let's take a look a
5 min read
Chain of Pointers in C with Examples
Prerequisite: Pointers in C, Double Pointer (Pointer to Pointer) in CA pointer is used to point to a memory location of a variable. A pointer stores the address of a variable.Similarly, a chain of pointers is when there are multiple levels of pointers. Simplifying, a pointer points to address of a v
5 min read
Function Pointer in C
In C, a function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs, and polymorphism (a concept where a function or operator behaves di
6 min read
How to Declare a Pointer to a Function?
A pointer to a function is similar to a pointer to a variable. However, instead of pointing to a variable, it points to the address of a function. This allows the function to be called indirectly, which is useful in situations like callback functions or event-driven programming. In this article, we
2 min read
Pointer to an Array | Array Pointer
A pointer to an array is a pointer that points to the whole array instead of the first element of the array. It considers the whole array as a single unit instead of it being a collection of given elements. Consider the following example: [GFGTABS] C #include<stdio.h> int main() { int arr[5] =
5 min read
Difference between constant pointer, pointers to constant, and constant pointers to constants
In this article, we will discuss the differences between constant pointer, pointers to constant & constant pointers to constants. Pointers are the variables that hold the address of some other variables, constants, or functions. There are several ways to qualify pointers using const. Pointers to
3 min read