Dereference Pointer in C
Accessing or manipulating the content that is stored in the memory address pointed by the pointer using dereferencing or indirection operator (*) is called dereferencing the pointer.
Syntax for Dereferencing a Pointer
We use the indirection operator (*) as the prefix to dereference a pointer:
*(pointer_name)
For modifying the data stored in the memory, we use
*(pointer_name) = new_value;
It is to be noted that the new_value must be of the same type as the previous.
Consider the above examples where ptr points to num, the content in the memory address can be accessed by the dereferencing operator *. Now, the *ptr will fetch the content stored in the address which is 10.
The num and ptr memory address and values will look like this.
Variable | Memory Address | Value |
---|---|---|
num = 10 | 202020 | 10 |
202021 | ||
202022 | ||
202023 | ||
ptr = &num | 202024 – 202032 | 202020 |
Note: We have assumed that the architecture in the above example is byte addressable i.e. minimum unit that has a separate address is a byte.
Dereference Pointer in C
We know that the pointer variable can store the memory address in C language and in this article, we will learn how to use that pointer to access the data stored in the memory location pointed by the pointer.