![]() |
VOOZH | about |
Dereferencing is, use of a pointer to access the value whose address is being stored. We use the * operator to get the value of the variable from its address.
*ptr, the compiler looks at the address stored in the pointer, goes to that memory location, and accesses or changes the actual data stored there not a copy.The address is : 0x7ffd3a5d27e4 Updated value is 10 Further updated value is 20 The value is 30
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.
There are 2 methods to change the value of pointers
One of the functionalities of the pointer is that it can store some other variable address. If we want to change the address to another variable in that case we can just reference the variable to another variable.
The address where a is stored is: 0x7ffc396e2838 The value stored at that address is: 10 Pointer is now pointing at: 0x7ffc396e283c New value the pointer is pointing to is: 20
In a few conditions, we can change the value of the actual variable when we need it rather than referencing another variable. In this, case we can just the dereferenced value to make actual changes.
The address where a is stored is: 0x7ffd32537efc The value stored at the address by dereferencing the pointer is: 5 Pointer is still pointing at: 0x7ffd32537efc The new value stored at the address by ...
An array is the collection of elements with similar data types, also the memory blocks where elements are continuous. We can use ptr to point to the elements of the array. Let's check how:
Elements of array: 1 2 3 4 5
The double pointers can also be dereferenced using the same logic but you will have to use the indirection operator two times: One for moving to the pointer the double pointer is pointing to and the other for accessing the actual value.
Accesing value from double pointer using **dptr: 10