
- C Programming Tutorial
- C - Home
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Booleans
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- C - Storage Classes
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- C - Functions
- C - Main Functions
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- C - Strings
- C - Array of Strings
- C - Special Characters
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Nested Structures
- C - Structure Padding and Packing
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
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
To Continue Learning Please Login