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.
// 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
Below are the programs to illustrate the various level of pointers:
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
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
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.
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
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.
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.
- Why do we need reference variables if we have pointers
- How to add many functions in one ng-click directive?
- How many types of memory areas are allocated by JVM?
- How to see the changes in whole directory/folder containing many sass files ?
- What are Wild Pointers? How can we avoid?
- Can a C++ class have an object of self type?
- Why C treats array parameters as pointers?
- Declare a C/C++ function returning pointer to array of integer pointers
- 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 in C/C++ with Examples
- Pointers vs References in C++
- What are near, far and huge pointers?
- Computing index using pointers returned by STL functions in C++
- 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++
- C++ Program to compare two string using 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 email@example.com. 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.