Dereference Operator in C (*) – Indirection operator in C Language

dereference-operator-in-c-or-indirection-operator-with-program

Introduction:

We have looked at the address of operator in c language in earlier posts, In today’s post we will look at the dereference operator in c programming language with an example program.

Prerequisites:

It is recommended to go through the Introduction to Pointers tutorial to better understand this article.

Dereference Operator in C Language:

So far we looked at how the pointer variable stores the address of another variable. We can access the value of the variable pointed by the pointer variable using the dereference operator (*).

The dereference operator is denoted by the Asterisk (‘*’) character. The dereference operator is also called as the indirection operator in C language.

📢 This Program is part of the Pointers Practice Programs series

Let’s say we have an integer variable mac with value 14

int mac = 14;

To store the address of the mac variable, We use an integer pointer like below.

int *macPtr = &mac;

Now, The macPtr holds the address of the mac variable.

The C language allows us to get the value of a variable by using its pointers. In this case, We can get the value of the mac variable by using the macPtr. To get the value of the variable using a pointer, We use the dereference operator i.e asterisk (*).

Use *macPtr to get the value of the mac variable.

printf("Value of mac is : %d\n", *macPtr);

Output:

Value of mac is : 14

📢 The dereference operator is also called as the indirection operator in C language.

Let’s look at an example to get the value of the variable indirectly using a pointer variable.

Get value of variable indirectly using pointer variable using dereferencing operator in C Language:

Here is the program to get the value of the integer variable using the pointer indirectly using indirection operator.

We have created an integer variable mac and assigned the value 14, Then we initialized a pointer variable macPtr and stored the mac variable address using address of operator(&)

Then we printed the value of the mac and the macPtr. The mac will contain the value 14 and macPtr will contain the memory address.

Finally, To indirectly access the value of the mac variable, We used the dereference operator or Indirection operator(*) with the macPtr pointer variable.

Program Output:

Let’s compile and run the program.

get-value-of-variable-indirectly-dereferencing-operator-on-pointer-in-c-language

As we can see from the above output, We were able to get the value of the variable mac variable using the macPtr pointer variable using the dereference operator.

Not only we can access the variable value using the pointer variable, We can also change the value of the variable using the pointer variable. Let’s look at another example program to change the variable value using pointer.

Modify Variable Value Indirectly using pointer with dereference Operator in C:

Here is the syntax to change the value of the variable using the pointer variable.

*PointerName = newValue;

Here the pointerName is the pointer which is pointing to a variable(non-constant). The newValue is the new value of the variable.

Here is the program to change/modify the variable value indirectly using the pointer variable in C programming language.

Program Output:

Compile the program using your compiler.

$ gcc update-value.c

Run the program.

change-value-of-variable-indirectly-using-pointer-in-c-programming

As we can see from the above output, The value of the bull variable is changed to 44.

Program Explanation – Change the value of a variable using the pointer:

We have defined an integer variable bulland the pointer variable bullPtr. Initially the bull variable has the value 88 and the pointer variable bullPtr storing the address of the bull variable.

int bull = 88;

int *bullPtr = •

Finally, we used the dereference operator (*) to modify/update the value of the bull variable using the bullPtr pointer variable like below.

*bullPtr = 44;

This changes the value of the bullvariable and updates it with the number 44. So the new value of the bull variable is 44 (which is modified indirectly using the pointer variable bullPtr).

Related Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

5 Responses

  1. […] Program to Understand the Dereference or Indirection Operator in C Language […]

  2. […] using pointers. To access the array’s elements, we will use standard pointer notation (by dereferencing the pointer) as well as pointer subscript […]

  3. […] using a pointer. To access the string elements, we will use standard pointer notation (by dereferencing the pointer) as well as pointer subscript […]

  4. […] Dereference Operator in C (*) – Indirection operator in C Language […]

  5. […] Dereference Operator in C (*) – Indirection operator in C Language […]

Leave a Reply