Dereferencing Pointers
Lesson 28Author : Afrixi
Last Updated : January, 2023
Dereferencing pointers is the process of accessing the value that a pointer is pointing to. When a pointer is declared, it is assigned a memory address where a value is stored. Dereferencing the pointer allows you to retrieve the value stored at that memory address.
To dereference a pointer in C, you use the *
operator. Here is an example:
#include <stdio.h>
int main() {
int var = 20; /* actual variable declaration */
int *ip; /* pointer variable declaration */
ip = &var; /* store address of var in pointer variable*/
printf("Value of var variable: %d\n", var );
printf("Address stored in ip variable: %p\n", ip );
printf("Value of *ip variable: %d\n", *ip );
return 0;
}
In this example, a pointer variable ip
is declared and assigned the memory address of the variable var using the &
operator. The *ip
statement inside the printf
statement retrieves the value stored at the memory address that ip
points to.
When you run this program, you will see the following output:
Value of var variable: 20
Address stored in ip variable: 0x7ffee5c33a4c
Value of *ip variable: 20
Notice that the address stored in the ip
variable is printed using %p
format specifier, and the value stored at that address is printed using %d
format specifier. The value printed using %p
represents the memory address in hexadecimal format.