
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Levels of Pointers in C/C++
In C/C++, the 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), so on.
This is useful in allocating memory dynamically, working with multi-dimensional arrays, and handling complicated data structures.
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/C++ to access or manipulate memory.
Syntax
Following is the syntax to the Single Level Pointer:
int a = 10; int *ptr = &a;
Example
This example demonstrates how a single pointer works with an integer variable.
#include <iostream> using namespace std; int main() { int a = 10; int *ptr = &a; cout << "Value of a: " << a << endl; cout << "Address of a: " << &a << endl; cout << "Pointer ptr stores address of a: " << ptr << endl; cout << "Value at ptr: " << *ptr << endl; return 0; }
The above program produces the following output:
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.
Syntax
Following is the syntax to the Double Level Pointer:
int a = 20; int *ptr = &a; int **pptr = &ptr;
Example
This example demonstrates how to declare and use a pointer to a pointer.
#include <iostream> using namespace std; int main() { int a = 20; int *ptr = &a; int **pptr = &ptr; cout << "Value of a: " << a << endl; cout << "Address of a: " << &a << endl; cout << "Value at ptr (address of a): " << ptr << endl; cout << "Value at pptr (address of ptr): " << pptr << endl; cout << "Value pointed by *pptr: " << *pptr << endl; cout << "Value pointed by **pptr: " << **pptr << endl; return 0; }
The above program obtained the following result:
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 (Multi-Level)
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.
Syntax
Following is the syntax to the Triple Level Pointer:
int a = 30; int *ptr = &a; int **pptr = &ptr; int ***ppptr = &pptr;
Example
In this example, we demonstrate the usage of a triple pointer to access a value through three levels of indirection.
#include <iostream> using namespace std; int main() { int a = 30; int *ptr = &a; int **pptr = &ptr; int ***ppptr = &pptr; cout << "Value of a: " << a << endl; cout << "Address of a: " << &a << endl; cout << "Value pointed by *pptr: " << *pptr << endl; cout << "Value pointed by **pptr: " << **pptr << endl; cout << "Value pointed by ***ppptr: " << ***ppptr << endl; return 0; }
Following is the output to the above program:
Value of a: 30 Address of a: 0x7ffc64f6697c Value pointed by *pptr: 0x7ffc64f6697c Value pointed by **pptr: 30 Value pointed by ***ppptr: 30