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.
// 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.
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.
// 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
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:
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.
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.
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Pointers in C/C++ with Examples
- Difference between Iterators and Pointers in C/C++ with Examples
- Why C treats array parameters as pointers?
- What are Wild Pointers? How can we avoid?
- Introduction of Smart Pointers in C++ and It's Types
- Dangling, Void , Null and Wild Pointers
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- Pointers vs References in C++
- What are near, far and huge pointers?
- Program to reverse an array using pointers
- The length of a string using pointers
- Output of C programs | Set 64 (Pointers)
- Applications of Pointers in C/C++
- Check if a string is palindrome in C using pointers
- Program to Reverse a String using Pointers
- Difference between pointer to an array and array of pointers
- C program to sort an array using pointers
- Features and Use of Pointers in C/C++
- How many levels of pointers can we have in C/C++
- Why do we need reference variables if we have pointers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.