• C Programming Video Tutorials

Dereference Pointer in C



In C language, the dereference operator (*) acts as a unary operator (unlike for multiplication where it acts as a binary operator), and it needs a pointer variable as its operand. The dereference operator returns the value stored in the variable that the pointer refers to.

A pointer stores the address of a variable. The address-of operator (&) is used to obtain the address of a variable. The type of the pointer variable must be the same as the variable whose address it stores.

 
int a = 10;
int *b = &a;

float x = 10.5;
float *y = &x;

What is Dereferencing?

The term "dereferencing" refers to accessing the value that is stored in the memory address referred by the pointer. The dereference operator (also called indirection operator) fetches the value of the target variable.

Example

In the above example, if we print "*b", you get the value of "a", i.e., 10. Similarly, printing "*y" displays 10.5.

#include <stdio.h>

int main (){

   int a = 10;
   int *b = &a;
   float x = 10.5;
   float *y = &x;

   printf ("Address of 'a': %d Value of 'a': %d\n", b, *b);
   printf ("Address of 'x': %d Value of 'x': %f\n", y, *y);

   return 0;
}

Output

Run the code and check its output −

Address of 'a': 6422028 Value of 'a': 10
Address of 'x': 6422024 Value of 'x': 10.500000

Using the Dereference Pointer to Update a Variable

The dereference operator also helps in indirectly manipulating the value of a variable referred to by a pointer.

Example

In this example, we change the value of "a" and "x" with the help of the dereference pointer −

#include <stdio.h>

int main (){

   int a = 10;
   int *b = &a;
   float x = 10.5;
   float *y = &x;

   *b = 100;
   *y = 100.50;

   printf ("Address of 'a': %d Value of 'a': %d\n", b, *b);
   printf ("Address of 'x': %d Value of 'x': %f\n", y, *y);

   return 0;
}

Output

Run the code and check its output −

Address of 'a': 6422028 Value of 'a': 100
Address of 'x': 6422024 Value of 'x': 100.500000

Dereferencing a Double Pointer

Just as you store the address of a normal variable in its pointer, you can have a pointer that stores the address of another pointer as well.

Let us declare a pointer to integer type and store the address of an integer variable in it.

int a = 10;
int *b = &a;

The dereference operator fetches the value via the pointer −

printf("a: %d \n Pointer to 'a' is 'b': %d \n Value at 'b': %d", a, b, *b);

The value of integer variable, its address, and the value obtained by the dereference pointer will be printed as −

a: 10 
Pointer to 'a' is 'b': 6422036 
Value at 'b': 10

Let us now declare a pointer that can store the address of "b", which itself is a pointer to the integer type written as "int *". Let's assume that the compiler also allocates it the address 3000. Hence, "c" is a pointer to a pointer to int and should be declared as "int **".

int **c = &b;
printf("b: %d \n Pointer to 'b' is 'c': %d \n Value at 'b': %d\n", b, c, *c);

You get the value of b (which is the address of a), the value of c (which is the address of b), and the dereferenced value from c (which is the address of a)

b: 6422036 
Pointer to 'b' is 'c': 6422024 
Value at 'b': 6422036

Since "c" is a double pointer here, the first asterisk in its declaration points to "b" and the second asterisk in turn points to "a". We can use the double reference pointer to obtain the value of "a" from "c".

printf("Value of 'a' from 'c': %d", **c);

This should display the value of a as 10.

Example

Try out the complete code given below −

#include <stdio.h>

int main (){

   int a = 10;
   int *b = &a;
   printf("a: %d \n Address: %d \n Value at 'a': %d\n\n", a, b, *b);

   int **c = &b;
   printf("b: %d \n Pointer to 'b' is 'c': %d \n Value at 'b': %d\n", b, c, *c);
   printf("Value of 'a' from 'c': %d", **c);

   return 0;
}

Output

When you run this code, it will produce the following output −

a: 10 
Address: 6422036 
Value at a: 10

b: 6422036 
Pointer to 'b' is 'c': 6422024 
Value at 'b': 6422036
Value of 'a' from 'c': 10

Dereferencing a Struct Pointer

The keyword "struct" is used to create a derived data type that consists of one or more elements of different types. Like a normal variable, you can declare a "struct pointer" and store its address.

struct book{
   char title[10];
   double price;
   int pages;
};

struct book b1 = {"Learn C", 650.50, 325};
struct book *ptr = &b1;

In C, the indirection operator represented by the arrow symbol (→) is used to obtain the values of the elements of the struct variable referred to by the struct pointer.

Example

"ptr -> title" returns the value of the title element, the same value returned by "b1.title". "ptr -> price" is equivalent to "b1.price", etc.

#include <stdio.h>

struct book{
   char title[10];
   double price;
   int pages;
};

int main (){

   struct book b1 = {"Learn C", 650.50, 325};
   struct book *ptr = &b1;

   printf("With -> Operator: \n");
   printf("Title: %s \nPrice: %7.2lf \nNumber of Pages: %d\n\n", ptr->title, ptr->price, ptr->pages);

   printf("With . Operator:\n");
   printf("Title: %s \nPrice: %7.2lf \nNumber of Pages: %d\n", b1.title, b1.price, b1.pages);

   return 0;
}

Output

When you run this code, it will produce the following output −

With -> Operator: 
Title: Learn C 
Price:  650.50 
Number of Pages: 325

With . Operator:
Title: Learn C 
Price:  650.50 
Number of Pages: 325

Dereferencing a Nested Struct Pointer

Even though C uses the arrow operator (→) to access the elements of a struct variable, the elements of any internal struct cannot be accessed with it.

Only the elements of the outer struct are accessible with the → operator. For subsequent inner struct elements, we need to use the dot (.) operator.

Example

The following example shows how you can dereference a nested struct pointer −

#include <stdio.h>
#include <string.h>

struct employee{
   char name[10];
   float salary;

   struct dob {
      int d, m, y;
   } d1;
};

int main(){

   struct employee e1 = {"Arjun", 45000, {12, 5, 1990}};
   struct employee *ptr = &e1;

   printf("Name: %s\n", ptr->name);
   printf("Salary: %f\n", ptr->salary);
   printf("Date of Birth: %d-%d-%d\n", ptr->d1.d, ptr->d1.m, ptr->d1.y);

   return 0;
}

Output

When you run this code, it will produce the following output −

Name: Arjun
Salary: 45000.000000
Date of Birth: 12-5-1990
Advertisements