Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Levels of Pointers in C/C++
In C, pointers have multiple levels, which means a pointer can point to another pointer − so the chains of indirection can go on and on. For instance, a pointer to a variable's address is stored at "*ptr" (single-level pointer) while, at "**ptr", the address of another pointer is kept (a double-level pointer), and so on.
This is useful in allocating memory dynamically, working with multi-dimensional arrays, and handling complicated data structures.
Syntax
// Single level pointer int *ptr = &variable; // Double level pointer int **pptr = &ptr; // Triple level pointer int ***ppptr = &pptr;
Following is the list of different levels of pointers. Let us understand these with the help of examples ?
Single Level Pointer
A single level pointer stores the address of a variable. It is the simplest form of pointer and used in C to access or manipulate memory.
Example
This example demonstrates how a single pointer works with an integer variable ?
#include <stdio.h>
int main() {
int a = 10;
int *ptr = &a;
printf("Value of a: %d\n", a);
printf("Address of a: %p\n", (void*)&a);
printf("Pointer ptr stores address of a: %p\n", (void*)ptr);
printf("Value at ptr: %d\n", *ptr);
return 0;
}
Value of a: 10 Address of a: 0x7ffe2458448c Pointer ptr stores address of a: 0x7ffe2458448c Value at ptr: 10
Double Level Pointer (Pointer to Pointer)
A double level pointer stores the address of another pointer. It is useful in cases like dynamic memory allocation and working with multidimensional arrays.
Example
This example demonstrates how to declare and use a pointer to a pointer ?
#include <stdio.h>
int main() {
int a = 20;
int *ptr = &a;
int **pptr = &ptr;
printf("Value of a: %d\n", a);
printf("Address of a: %p\n", (void*)&a);
printf("Value at ptr (address of a): %p\n", (void*)ptr);
printf("Value at pptr (address of ptr): %p\n", (void*)pptr);
printf("Value pointed by *pptr: %p\n", (void*)*pptr);
printf("Value pointed by **pptr: %d\n", **pptr);
return 0;
}
Value of a: 20 Address of a: 0x7ffd2b133524 Value at ptr (address of a): 0x7ffd2b133524 Value at pptr (address of ptr): 0x7ffd2b133528 Value pointed by *pptr: 0x7ffd2b133524 Value pointed by **pptr: 20
Triple Level Pointer
A triple level pointer stores the address of a double pointer. It is rarely used but becomes relevant in very complex data handling or passing multi-level pointer references to functions.
Example
In this example, we demonstrate the usage of a triple pointer to access a value through three levels of indirection ?
#include <stdio.h>
int main() {
int a = 30;
int *ptr = &a;
int **pptr = &ptr;
int ***ppptr = &pptr;
printf("Value of a: %d\n", a);
printf("Address of a: %p\n", (void*)&a);
printf("Value pointed by *pptr: %p\n", (void*)*pptr);
printf("Value pointed by **pptr: %d\n", **pptr);
printf("Value pointed by ***ppptr: %d\n", ***ppptr);
return 0;
}
Value of a: 30 Address of a: 0x7ffc64f6697c Value pointed by *pptr: 0x7ffc64f6697c Value pointed by **pptr: 30 Value pointed by ***ppptr: 30
Key Points
- Each additional level of pointer adds one more
*in declaration and dereferencing. - Double pointers are commonly used for dynamic 2D arrays and function parameters that modify pointer values.
- Triple and higher-level pointers are rarely needed in practice but follow the same pattern.
Conclusion
Multi-level pointers in C provide flexibility for complex data structures and memory management. While single and double pointers are most commonly used, understanding the concept helps in advanced programming scenarios requiring multiple levels of indirection.
