C++

How to Dereference a Pointer C++?

Dereferencing is a technique for getting data from a memory address pointed by a pointer mutable and manipulating it. A mutable that holds the address of some other mutable is known as a pointer. The (*) sign will be used as the dereference operator. Dereferencing a pointer occurs whenever the operator (*) is being cast off in the pointer mutable. Whenever a pointer is dereferenced, the value of the mutable represented by the pointer is reverted. Therefore, we will be discussing the concept of dereferencing a pointer in C++ while utilizing the Ubuntu 20.04 system. Let’s have a fresh start with creating a file in Ubuntu using the “touch” terminal command. The file will be created within your machine’s root “home” directory. It can be opened in any of the editors you have in your system. We prefer the “nano” editor to open the empty file within the terminal console.

$ touch dereference.cc
$ nano dereference.cc

Example 01:

We have to start with including a library “iostream” for input-output stream usage in C++. The #include keyword is utilized to do so. Then, a standard namespace “std” came to let us use the standard cout and cin statements. All the work has been performed within the main() method. We have initialized an integer variable “n” with the value 1. An integer type pointer “p” is used for reference to the variable “n” address via the “&” operator. The first cout statement is being used to display the value of variable “n” on the shell via the pointer “p.” The next cout statement is utilized to show the value of a variable “n” on the shell.

#include<iostream>
using namespace std;
int main(); {
int n = 1;
int* p = &n;
cout<<"Address of n: "<<p<<endl;
cout<<"Value of n: "<<*p<<endl;
}

You have to compile the new code with Ubuntu’s newly installed g++ compiler for the C++ language. As of now, no errors have been recognized. After this, we have executed the code with the “./a.out” command and got the address & value of variable “n” using the pointer dereference.

$ g++ dereference.cc
$ ./a.out

Example 02:

Let’s have another example a little different from the above example. So, the example has been started with the same input-output stream library header and the “std” standard namespace with the “using” keyword. Now, the main() function is started with the declaration and initialization of a string variable “v” with the string value “Linux.” After this, we have declared a string-type pointer to perform the dereferencing and relate it to the variable “v” via the “&” operator. The dereferencing is utilized here to fetch the value from a variable through the pointer. The only change is the value of a variable, i.e., string instead of an integer. The first cout clause is used to display the address of a variable “v,” and the other cout statement is utilized to display the value of variable “v.” Let’s save the script and execute our code.

#include<iostream>
using namespace std;
int main(); {
string v = "Linux";
string *p = &v;
cout<<"Address of v: "<<p<<endl;
cout<<"Value of v: "<<*p<<endl;
}

The code has been compiled. After this, we have executed the simple code and got the address and value of a variable “v” upon using the dereferencing method via the pointer.

$ g++ dereference.cc
$ ./a.out

Example 03:

Let’s have our last but not the least code started with the library header, i.e., iostream, and the standard namespace “std” once again. We have declared two integer variables within the main() function, v1, and v2. While v1 is initialized with the value “5”. The first two cout statements display the current values of v1 and v2 on the shell, and the third one displays the pointer “p” address. The next cout statement tells us that we are going to perform dereferencing now. So, we have used the “p=&v1” statement to assign the address of a variable v1 to pointer p. This is called dereferencing. The pointer “p” is used to show the address and value of variable v1 on the shell via the cout clauses. Now, we are assigning the value of the pointer “p” (which is v1) to variable v2. The cout statement will display the value of v2, and the pointer “p” is assigned with a new value of 10. The last 4 statements show the new or current values of v1, v2, and pointer “p.”

#include<iostream>
using namespace std;
int main(); {
int v1 = 5, v2;
cout<<"Value of v1: "<<v1<<endl;
cout<<"Value of v2: "<<v2<<endl;
int *p;
cout<<"Value of pointer p: "<<p<<endl;
cout<<"************* After using p=&v1 ************** "<<endl;
p=&v1;
cout<<"Address of v1: "<<p<<endl;
cout<<"Value of v1: "<<*p<<endl;
v2=*p;
cout<<"************* After using v2=*p ************** "<<endl;
cout<<"Address of v2: "<<v2<<endl;
*p=10;
cout<<"************* After using *p=10 ************** "<<endl;
cout<<"Address of p: "<<p<<endl;
cout<<"Value of p: "<<*p<<endl;
cout<<"Value of v1: "<<v1<<endl;
cout<<"Value of v2: "<<v2<<endl;
}

The start values of v1 & v2 have been displayed along with the pointer address. After “p=&v1”, the address of “p” is replaced with that of v1. After “v2=*p”, a ” p ” reference value is assigned to v2. Using “*p=10”, the value of pointer “p” becomes 10, and due to dereferencing, v1 also becomes 10. While v2 and the address of the pointer are the same.

$ g++ dereference.cc
$ ./a.out

Conclusion:

So, this was all about how to dereference a pointer in C++. We have used this concept to fetch the values of a variable and change or update the value. We hope that we successfully provide you with simple and easy-to-understand content.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.