A pointer is a variable that stores the address of another variable, as we already know from “what is a pointer.” The (*) represents the dereference operator, which is also known as an indirection operator.
Dereferencing a pointer occurs when the indirection operator (*) is used with the pointer variable. When a pointer is dereferenced, the value of the variable pointed by the pointer is returned.
What is the purpose of using a dereferencing pointer?
Because of the following reasons, dereference a pointer is used:
- It can be used to read or manipulate data stored at the memory location that the pointer is pointing to.
- Any operation performed on the dereferenced pointer has a direct impact on the value of the variable to which it refers.
Let’s observe the following steps to dereference a pointer:
- First, we declare the integer variable to which the pointer points.
int x =9;
- Now, we declare the integer pointer variable.
int *ptr;
- We keep the address of the ‘x’ variable in the pointer variable ‘ptr’ after declaring an integer pointer variable.
ptr=&x;
- By dereferencing a pointer ‘ptr,’ we can modify the value of the ‘x’ variable as seen below:
*ptr =8;
Because ‘ptr’ links to the ‘x’ location and dereferencing ‘ptr’, i.e., *ptr=8, updates the value of x, the above line changes the value of x from 9 to 8.
Let’s combine all the above steps:
#include <stdio.h> int main() { int x=9; int *ptr; ptr=&x; *ptr=8; printf("value of x is : %d", x); return 0; }
Output:
value of x is : 8
Let’s consider another example.
#include <stdio.h> int main() { int x=4; int y; int *ptr; ptr=&x; y=*ptr; *ptr=5; printf("The value of x is : %d",x); printf("\n The value of y is : %d",y); return 0; }
- We define two variables, ‘x’ and ‘y,’ with ‘x’ having a value of ‘4’.
‘ptr’ is a pointer variable that we declare. - We assign the address of the ‘x’ variable to the pointer ‘ptr’ after declaring a pointer variable.
- Because the ‘ptr’ variable includes the address of the ‘x’ variable, ‘*ptr’ is the same as ‘x’.
- Instead of utilizing the ‘x’ variable, we use the ‘ptr’ variable to assign the value of ‘x’ to ‘y,’ i.e. y=*ptr.
We believe that if we alter the value of ‘x,’ we will also change the value of ‘y,’ because the pointer ‘ptr’ carries the location of the ‘x’ variable. However, because ‘y’ is storing a local copy of value ‘5,’ this does not happen.
Note
Output:
The value of x is : 4
The value of y is : 5
Let’s consider another scenario.
#include <stdio.h> int main() { int a=90; int *ptr1,*ptr2; ptr1=&a; ptr2=&a; *ptr1=7; *ptr2=6; printf("The value of a is : %d",a); return 0; }
Output:
- We begin by declaring an ‘a’ variable.
- Then we declare two pointers, ptr1 and ptr2, respectively.
- The address of the ‘a’ variable is contained in both pointers.
- The value ‘7’ is assigned to *ptr1 and ‘6’ to *ptr2. ‘a’ would have a final value of ‘6.’
Note that: if there are multiple pointers going to the same location, the change made by one pointer will be the same as the change made by another pointer.
If you want to improve this post please connect us.