How many levels of pointers can we have in C/C++
Prerequisite: Pointer in C and 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 and the value of a variable can be accessed using dereferencing of the pointer.
A pointer is generally initialized as:
datatype *variable name;
This above declaration is a single pointer but there can be more than this. This is called levels of pointers. According to ANSI C, each compiler must have at least 12 levels of pointers. This means we can use 12 * symbols with a variable name.
Level Of Pointers in C/C++:
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.
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
Below are the programs to illustrate the various level of pointers:
Program 1:
C
// C program to illustrate levels of pointer #include <stdio.h> // Driver Code int main() { int var = 10; // Pointer level-1 // Declaring pointer to var int * ptr1; // Pointer level-2 // Declaring pointer to pointer // variable *ptr1 int ** ptr2; // Pointer level-3 // Declaring pointer to double // pointer **ptr2 int *** ptr3; // Storing address of var // to pointer variable ptr1 ptr1 = &var; // Storing address of pointer // ptr1 to level-2 ptr2 ptr2 = &ptr1; // Storing address of level-2 // ptr2 to level-3 pointer ptr3 ptr3 = &ptr2; // Displaying values 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; } |
C++
// C++ program to illustrate // levels of pointer #include <bits/stdc++.h> using namespace std; // Driver Code int main() { int var = 10; // Pointer level-1 // Declaring pointer to var int * ptr1; // Pointer level-2 // Declaring pointer to pointer // variable *ptr1 int ** ptr2; // Pointer level-3 // Declaring pointer to double // pointer **ptr2 int *** ptr3; // Storing address of var // to pointer variable ptr1 ptr1 = &var; // Storing address of pointer // ptr1 to level-2 ptr2 ptr2 = &ptr1; // Storing address of level-2 // ptr2 to level-3 pointer ptr3 ptr3 = &ptr2; // Displaying values cout << "Value of variable var is " << var << endl; cout << "Value of variable var " << "using pointer ptr1 is " << *ptr1 << endl; cout << "Value of variable var " << "using pointer ptr2 is " << **ptr2 << endl; cout << "Value of variable var " << "using pointer ptr3 is " << ***ptr3 << endl; return 0; } |
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
Program 2:
C
// C program to illustrate // levels of pointer #include <stdio.h> // Driver Code int main() { float var = 23.564327; // Declaring pointer variables // upto level_4 float *ptr1, **ptr2, ***ptr3, ****ptr4; // Initializing pointer // variables ptr1 = &var; ptr2 = &ptr1; ptr3 = &ptr2; ptr4 = &ptr3; // Printing values 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; } |
C++
// C++ program to illustrate // levels of pointer #include <bits/stdc++.h> using namespace std; // Driver Code int main() { float var = 23.564327; // Declaring pointer variables // upto level_4 float *ptr1, **ptr2, ***ptr3, ****ptr4; // Initializing pointer // variables ptr1 = &var; ptr2 = &ptr1; ptr3 = &ptr2; ptr4 = &ptr3; // Printing values cout << "Value of var is " << var << endl; cout << "Value of var using level-1" << " pointer is " << *ptr1 << endl; cout << "Value of var using level-2" << " pointer is " << **ptr2 << endl; cout << "Value of var using level-3" << " pointer is " << ***ptr3 << endl; cout << "Value of var using level-4" << " pointer is " << ****ptr4 << endl; return 0; } |
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
Explanation:
The above code where we have taken float data type of the variable, so now we have to take the 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.
Program 3:
C
// C program to illustrate // levels of pointer #include <stdio.h> // Driver Code int main() { // Initializing integer variable int var = 10; // Declaring pointer variables // upto level-3 int *ptr1, **ptr2, ***ptr3; // Initializing pointer variables ptr1 = &var; ptr2 = &ptr1; ptr3 = &ptr2; // Printing values BEFORE updation 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); // Updating var's value using // level-3 pointer ***ptr3 = 35; // Printing values AFTER updation 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; } |
C++
// C++ program to illustrate // levels of pointer #include <bits/stdc++.h> using namespace std; // Driver Code int main() { // Initializing integer variable int var = 10; // Declaring pointer variables // upto level-3 int *ptr1, **ptr2, ***ptr3; // Initializing pointer variables ptr1 = &var; ptr2 = &ptr1; ptr3 = &ptr2; // Printing values BEFORE updation cout << "Before:" << endl; cout << "Value of var is " << var << endl; cout << "Value of var using level-1" << " pointer is " << *ptr1 << endl; cout << "Value of var using level-2" << " pointer is " << **ptr2 << endl; cout << "Value of var using level-3" << " pointer is " << ***ptr3 << endl; // Updating var's value using // level-3 pointer ***ptr3 = 35; // Printing values AFTER updation cout << "After:" << endl; cout << "Value of var is " << var << endl; cout << "Value of var using level-1" << " pointer is " << *ptr1 << endl; cout << "Value of var using level-2" << " pointer is " << **ptr2 << endl; cout << "Value of var using level-3" << " pointer is " << ***ptr3 << endl; return 0; } |
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
Explanation:
As we already know that a pointer points to address the location of a variable so when we access the value of a pointer that points to the variable’s value. Now to update the value of the variable, we can use any level of pointer as ultimately every pointer is directly or indirectly pointing to that variable only. It will directly change the value present at the address location of the variable.