1.
Introduction
2.
Demonstration
2.1.
Code
3.
Examples of dereferencing pointers
3.1.
Example 1
3.2.
Example 2
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

# Dereference Pointers

ANKIT KUMAR
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

## Introduction

Pointers are also variables, but instead of storing a value like normal variables, they store the address of some other variable. We know that the pointers hold memory addresses of some other variables. But how can we get the value or data which is stored in the variable? The solution is to dereference the pointer.

Dereferencing is a technique for accessing or manipulating data stored in a memory location pointed to by a pointer. We use the * symbol with the pointer variable when dereferencing the pointer variable.

Using dereferencing, we can get the value inside the variable.

Also see: C Static Function, And Tribonacci Series

## Demonstration

Now that we have defined what is meant by dereferencing a pointer, it's time to see it practically using code.

• Step 1: declare a variable and assign it a value.

int var =100;

• Step 2: Declare a pointer variable that will point to the variable var which we created above.

int *p;

• Step 3: Store the address of variable var in the pointer variable ptr. This is known as referencing.

p= &var;

• Step 4: We can now manipulate the value in variable var by dereferencing the pointer variable p, as shown below.

*p= 200;

This will now change the value of var from 100 to 200.

The pointer points to the memory address of the variable var. When we write p=&var, we let the pointer know the address of var. Now the pointer is holding the address of var. When we write *p=200, the * symbol tells the pointer to change the value of the variable.

### Code

``````#include <stdio.h>
int main()
{
// declare a variable var and assign it a value 100
int var = 100;

// declare a pointer variable.
int *p;

// store the address of var
p = &var;

// change the value of var using dereferencing
// *p=200 is equivalent to var=200
// here without using var we change the value of var using the pointer
*p = 200;

// print the changed value of var
printf("value of var is : %d", var);
return 0;

}  ``````

Output:

``value of var is : 200``

You can also read about the dynamic arrays in c and  Short int in C Programming

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## Examples of dereferencing pointers

Now that we have fully understood the concept of pointer dereference let's see some more examples of it with proper code implementation.

### Example 1

``````#include <stdio.h>
int main()
{
// declare a variable a and assign a value 100 to it.
int a =100;
// print the initial value of a. (100)
printf("Initial value of a is : %d",a);

// declare another variable b.
int b;
// declare a pointer variable ptr.
int *ptr;

// assign the address of variable a in ptr
ptr=&a;

// b stores the value of the variable pointed by ptr which is a.
// here *ptr will return the value of a, which is equal to 100.
// therefore b stores 100
b=*ptr;

//*ptr changes the value of a from 100 to 1000.
*ptr=1000;

// print the changed value of a
printf("\nThe final value of a is : %d",a);

// print the value of b
printf("\nThe value of b is : %d",b);
return 0;
}  ``````

Output:

``````Initial value of a is : 100
The final value of a is : 1000
The value of b is : 100``````

Explanation:

• We first declare a variable â€śaâ€ť and assign a value 100 to it.
• We then declare another variable, "b."
• We then declare a pointer variable ptr and assign the address of the variable "a" in ptr.
• Using *ptr, we are able to access the value of variable a. ptr holds the address; however, *ptr will return the value of the variable to which it is pointing.
• Using b= *ptr, we put 100 in b.
• Using *ptr= 1000, we change the value of the variable pointed by ptr to 1000. This way, the value of variable a is changed to 1000.

### Example 2

``````#include <stdio.h>
int main()
{
// declare a variable var and assign 100 to it.
int var=100;

// print initial value of variable var
printf("The initial value of var is : %d",var);

// declare two different pointers ptr1 and ptr2
int *ptr1,*ptr2;

// assign the address of var to ptr1 and ptr2
ptr1=&var;
ptr2=&var;

// change the value of var using pointer dereference
//*ptr1 changes value of var to 2021
*ptr1=2021;

// *ptr2 changes the value of var to 2022
*ptr2=2022;

// output the values
printf("\nThe changed value of var is : %d",var);
printf("\n*ptr1 will print : %d",*ptr1);
printf("\n*ptr2 will print : %d",*ptr2);
return 0;
}  ``````

Output:

``````The initial value of var is : 100
The changed value of var is : 2022
*ptr1 will print : 2022
*ptr2 will print : 2022``````

You can practice by yourself with the help of a C editor online.

Explanation

• First, we declare a variable var.
• We declare two pointers that point to the same variable var.
• Using pointer dereferencing, we change the value of var using *ptr1.
• Similarly, we change the value of var using *ptr2.
• The final value of var is the value that was assigned to it using *ptr2.

## FAQs

1.What is meant by dereferencing a pointer?

Dereferencing is a technique for accessing or manipulating data stored in a memory location pointed to by a pointer.

2. How can we manipulate the value of a variable by dereferencing a pointer?

We use the * symbol with the pointer variable when dereferencing the pointer variable.

3. What is the dereference operator?

The * symbol is the dereference operator, which is used with the pointers.

4. Why do we need to dereference a pointer?

The data stored in the variable can be accessed and manipulated very quickly, and also dereferencing affects the variable directly without having to make a copy of it.

## Key Takeaways

In this article, we have extensively discussed dereference pointers with various code examples in the C programming language.

• Dereferencing is a technique for accessing or manipulating data stored in a memory location pointed to by a pointer.
• We use the * symbol with the pointer variable when dereferencing the pointer variable.

We hope that this blog has helped you enhance your knowledge regarding dereference pointers and if you would like to learn more, check out our articles here. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass