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.
C++ #include <bits/stdc++.h> using namespace std; int main() { int var = 10; int* pt; pt = &var; cout << "The address is : " << pt << endl; // We get value by dereferencing cout << "Updated value is " << *pt << endl; // Update value using dereferencing *pt = 20; cout << "Further updated value is " << var << endl; var = 30; // Access new updated value using // dereferncing cout << "The value is " << *pt << endl; } OutputThe address is : 0x7ffd1ad1d5b4 Updated value is 10 Further updated value is 20 The value is 30

Changing the value of the Pointer
There are 2 methods to change the value of pointers
- Change the address of the stored variable
- Change the value in the stored variable
When do you want to assign another address to the pointer?
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.
C++ // C++ Program to changing the // address pointed by pointer #include <iostream> using namespace std; // Driver code int main() { int a = 10, b = 20; int* pt; // Referencing to the pointer pt = &a; cout << "The address where a is stored is: " << pt << endl; // dereference the pointer cout << "The value stored at the address by " "dereferencing the pointer is: " << *pt << endl; // Referening the address to same pointer pt = &b; // dereference the pointer cout << "Pointer is now pointing at: " << pt << endl; cout << "New value the pointer is pointing to is: " << *pt << endl; return 0; } OutputThe address where a is stored is: 0x7fff1b3ce9a4 The value stored at the address by dereferencing the pointer is: 10 Pointer is now pointing at: 0x7fff1b3ce9a0 New value the pointer is pointing to is:...
When you want to change the value of the variable by dereference operator?
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.
Example:
int a=5;
// Referencing
int *ptr=&a;
// Dereferencing and changing value
*ptr=6;
Below is the implementation of the above example:
C++ // C++ Program to change the value // of variable using dereferncing of // pointers #include <iostream> using namespace std; int main() { int a = 5, b = 6; // Pointer declared int* pt; // Referencing pt = &a; cout << "The address where a is stored is: " << pt << endl; cout << "The value stored at the address by " "dereferencing the pointer is: " << *pt << endl; // Changing value of variable // Address stored in the pointer will not // be effected by this *pt = b; cout << "Pointer is still pointing at: " << pt << endl; cout << "The new value stored at the address by " "dereferencing the pointer is: " << *pt << endl; cout << "Now the value of a is: " << a << endl; return 0; } OutputThe address where a is stored is: 0x7ffe9c47bc1c The value stored at the address by dereferencing the pointer is: 5 Pointer is still pointing at: 0x7ffe9c47bc1c The new value stored at the address by ...
Using Array with Pointers
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:
Example:
int arr=[1,2,3,4,5];
// Referencing with array
int *ptr=arr;
// Dereferencing the array // 1 will be printed
cout<< *ptr <<" ";
Below is the implementation of the above example:
C++ // C++ Program to implement // Array using Pointer #include <iostream> using namespace std; int main() { // Array Declared int arr[] = { 1, 2, 3, 4, 5 }; // Referencing array with pointer int* ptr = arr; // Using dereferencing to print // elements of array cout << "Elements of array:"; for (int i = 0; i < 5; i++) { cout << *(ptr + i) << " "; } cout << endl; return 0; } OutputElements of array:1 2 3 4 5
Explore
C++ Basics
Core Concepts
OOP in C++
Standard Template Library(STL)
Practice & Problems
My Profile