C dereference pointer

Dereferencing a pointer means getting the value that is stored in the memory location pointed by the pointer. The dereference operator * also known as indirection operator is used to do this, and is called the dereferencing operator. When indirection operator (*) is used with the pointer variable, then it is known as dereferencing a pointer.

Usage of dereferencing pointer

Dereference a pointer is used because of the following reasons.

  • It can be used to access or manipulate the data stored at the memory location, which is pointed by the pointer.
  • Any operation applied to the dereferenced pointer will directly affect the value of the variable that it points to.
Example #1

In the below example steps to dereference a pointer is given.

1) Declare the integer variable to which the pointer points.

snippet
int x =9;

2) Declare the integer pointer variable.

snippet
int *ptr;

3) Store the address of 'x' variable to the pointer variable 'ptr'.

snippet
ptr=&x;

Change the value of 'x' variable by dereferencing a pointer 'ptr' as given below:

snippet
*ptr =8;

The above code changes the value of 'x' variable from 9 to 8 because 'ptr' points to the 'x' location and dereferencing of 'ptr', i.e., *ptr=8 will update the value of x.

Below example combines all the above steps

snippet
#include <stdio.h>
int main()
{
    int x=9;
    int *ptr;
    ptr=&x;
    *ptr=8;
    printf("value of x is : %d", x);
    return 0;}

Output

C dereference pointer
Example #2

Let's consider another example.

snippet
#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 above code.

  • We declare two variables 'x' and 'y' where 'x' is holding a '4' value.
  • We declare a pointer variable 'ptr'.
  • Assign the address of the 'x' variable to the pointer 'ptr'.
  • As the 'ptr' contains the address of 'x' variable, so '*ptr' is the same as 'x'.
  • Now assign the value of 'x' to 'y' with the help of 'ptr' variable, i.e., y=*ptr instead of using the 'x' variable.
Note
If we change the value of 'x', then the value of 'y' will also get changed as the pointer 'ptr' holds the address of the 'x' variable. But this does not happen, as 'y' is storing the local copy of value '5'.

Output

C dereference pointer

Let's consider another scenario.

snippet
#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 above code:

  • Both the pointers ptr1 and ptr2 contain the address of 'a' variable.
  • So when we assign the '7' value to the *ptr1 and '6' to the *ptr2. The final value of 'a' would be '6'.
Note
If we have more than one pointer pointing to the same location, then the change made by one pointer will be the same as another pointer.

Output

C dereference pointer
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +