We already know “what is a pointer?”, a pointer is a type of variable that stores the address of a variable. Dereference is also called an indirection operation and is represented as (*).
When the indirection operation (*) is employed with the variable pointer, it is called dereferencing the pointer. When we dereference an object, what is in the variables that are pointed by this pointer is returned.
Why we use dereferencing pointer?
Dereference, also known as a pointer, is utilized for one of the reasons listed below:
- It is able to access or alter the information stored in the memory locationthat is indicated to by the pointer.
- Any change made to the dereferenced object will directly impact what the variables it is pointing to.
Let’s take a look at the following steps for dereferencing an object.
- In the beginning, we define the integer variable that the pointer is pointing to.
int x =9;
- We now create the variable pointer integer.
int *ptr;
- Following declaring an int pointer variable we record the address of the ‘x variable in the pointer variable “ptr”.
ptr=&x;
- You can alter the value of the variable ‘x’ by using a pointer “ptr” as shown below:
*ptr =8;
The above line alters the value of the ‘x variable from 9 to 8. since ‘ptr’ is the location of ‘x’ and dereferencing the ‘ptr’ variable, i.e. *ptr=8 would update the value of the variable x.
Let’s combine all of the previous 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
Let’s take another look.
#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;
}
In the code above:
- Two variables are declared “x” and “y”, where ‘x’ represents an ‘4’ number.
- We declare a pointer variable ‘ptr’.
- After declaring the pointer variable We determine the location of the variable ‘x’ to the pointer variable ‘ptr’.
- We know that “ptr” contains the address of the ‘x variables, and “*ptr” is exactly identical to ‘x’..
- The value assigned by “x” to “y” using the “ptr” variable. i.e. that the value of y is* ptr instead of using the variable ‘x.
Note: As per our research if we alter values of the variable ‘x’ this means that the value of the ‘y’ would be changed too since the pointer ‘ptr’ contains its address to the variable ‘x. This isn’t the case since ‘y’ stores an internal copy of the value “5”.
Output
Let’s look at 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;
}
In the code above:
- Then we define an “a variable.
- We then define two pointers i.e. the ptr1 pointer and the ptr2.
- Both of the pointers include the address of the ‘a’ variable.
- The 7 value to the *ptr1 while ‘6’ is assigned for the *ptr2. The final value for “a” would be “6′.
Note: If there are multiple pointers pointing to the same spot the changes that one pointer makes will be identical to the change made by another pointer.
Output