”;
Dereference Pointer in C
The dereference operator is used to access and manipulate the value stored in the variable pointed by the pointer. The dereference or indirection operator (*) acts as a unary operator, and it needs a pointer variable as its operand.
Syntax
Below is the syntax to dereference a pointer −
*pointer_variable;
With the help of the above syntax (dereference pointer), you can get and update the value of any variable that is pointing by the pointer.
How to Dereference a Pointer?
To dereference a pointer, you need to follow the below-given steps:
- Create a variable and declare a pointer variable.
- Initialize the pointer by assigning the address of the variable.
- Now, you can dereference the pointer to get or update the value of the variable.
Example
In this example, we are demonstrating these three steps to deference a pointer −
#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 = %dn", *ptr); return 0; }
Output
Run the code and check its output −
Value of x = 10
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'': %dn", b, *b); printf ("Address of ''x'': %d Value of ''x'': %fn", 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
Manipulating Value by Dereferencing Pointer
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'': %dn", b, *b); printf ("Address of ''x'': %d Value of ''x'': %fn", 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. A pointer having address of another pointer is known as double pointer or pointer-to-pointer.
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'': %dn", 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'': %dnn", a, b, *b); int **c = &b; printf("b: %d n Pointer to ''b'' is ''c'': %d n Value at ''b'': %dn", 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 Structure 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 structure 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: %dnn", ptr->title, ptr->price, ptr->pages); printf("With . Operator:n"); printf("Title: %s nPrice: %7.2lf nNumber of Pages: %dn", 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 Structure Pointer
Even though C uses the arrow operator (→) to access the elements of a structure 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: %sn", ptr->name); printf("Salary: %fn", ptr->salary); printf("Date of Birth: %d-%d-%dn", 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
”;