Understanding Dereference Pointers in C: Accessing Variable Values
Learn about the dereference operator in C, which allows you to access and manipulate the value stored in the variable a pointer points to. Discover the syntax for dereferencing pointers using the *
operator and follow simple steps to get or update variable values efficiently.
Dereference Pointer in C
The dereference operator is used to access and manipulate the value stored in the variable that a pointer points to. The dereference (or indirection) operator *
acts as a unary operator and requires a pointer variable as its operand.
Syntax
Below is the syntax to dereference a pointer:
*pointer_variable;
Using the above syntax, you can get and update the value of any variable that the pointer is referencing.
How to Dereference a Pointer?
To dereference a pointer, follow these steps:
- Create a variable and declare a pointer variable.
- Initialize the pointer by assigning the address of the variable.
- Dereference the pointer to get or update the value of the variable.
Example
In this example, we demonstrate these three steps:
Complete Example
#include <stdio.h>
int main() {
// Create a variable and pointer variable
int x = 10;
int *ptr;
// Initialize the pointer by assigning the address of the variable
ptr = &x;
// Dereference the pointer
printf("Value of x = %d\n", *ptr);
return 0;
}
Output
Value of x = 10
What is Dereferencing?
The term "dereferencing" refers to accessing the value stored at the memory address referenced by the pointer. The dereference operator fetches the value of the target variable.
Example
In the following example, printing *b
retrieves the value of a
, which is 10. Similarly, printing *y
yields 10.5:
Complete Example
#include <stdio.h>
int main() {
int a = 10;
int *b = &a;
float x = 10.5;
float *y = &x;
printf("Address of 'a': %p Value of 'a': %d\n", (void*)b, *b);
printf("Address of 'x': %p Value of 'x': %f\n", (void*)y, *y);
return 0;
}
Output
Address of 'a': 0x7ffeefbff5ac Value of 'a': 10
Address of 'x': 0x7ffeefbff5a8 Value of 'x': 10.500000
Manipulating Value by Dereferencing Pointer
The dereference operator also allows for indirect manipulation of the value of a variable referenced by a pointer.
Example
In the following example, we change the values of a
and x
using the dereference pointer:
Complete Example
#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': %p Value of 'a': %d\n", (void*)b, *b);
printf("Address of 'x': %p Value of 'x': %f\n", (void*)y, *y);
return 0;
}
Output
Address of 'a': 0x7ffeefbff5ac Value of 'a': 100
Address of 'x': 0x7ffeefbff5a8 Value of 'x': 100.500000
Dereferencing a Double Pointer
Just as you can store the address of a normal variable in a pointer, you can also have a pointer that stores the address of another pointer, known as a double pointer or pointer-to-pointer.
Example
Let’s declare a pointer to an integer type and store the address of an integer variable:
int a = 10;
int *b = &a;
The dereference operator can be used to fetch the value via the pointer:
printf("a: %d \n Pointer to 'a' is 'b': %p \n Value at 'b': %d", a, (void*)b, *b);
Now, let’s declare a pointer that can store the address of b
, which is itself a pointer to an integer type:
int **c = &b;
You can print the values as follows:
printf("b: %p \n Pointer to 'b' is 'c': %p \n Value at 'b': %p\n", (void*)b, (void*)c, *c);
Complete Example
Here’s a complete code example demonstrating the use of double pointers:
Complete Example
#include <stdio.h>
int main() {
int a = 10;
int *b = &a;
printf("a: %d \n Address: %p \n Value at 'a': %d\n\n", a, (void*)b, *b);
int **c = &b;
printf("b: %p \n Pointer to 'b' is 'c': %p \n Value at 'b': %p\n", (void*)b, (void*)c, *c);
printf("Value of 'a' from 'c': %d", **c);
return 0;
}
Output
a: 10
Address: 0x7ffeefbff5ac
Value at 'a': 10
b: 0x7ffeefbff5ac
Pointer to 'b' is 'c': 0x7ffeefbff5a8
Value at 'b': 0x7ffeefbff5ac
Value of 'a' from 'c': 10
Dereferencing a Structure Pointer
The keyword struct
is used to create a derived data type that consists of one or more elements of different types. You can declare a structure pointer and store its address like a normal variable.
Example
Here’s an example of declaring a structure and using a pointer to access its members:
Complete Example
#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
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 Structure Pointer
While C uses the arrow operator (→
) to access the elements of a structure variable, the elements of any internal structure cannot be accessed using this operator. Only the elements of the outer structure are accessible with →
. For subsequent inner structure elements, you need to use the dot (.
) operator.
Example
The following example demonstrates how to dereference a nested structure pointer:
Complete Example
#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
Name: Arjun
Salary: 45000.000000
Date of Birth: 12-5-1990