The concept of dereferenced pointer:
Dereference pointer is also called value at an address that clearly describes its sole purpose, referring to the value at the given address. Dereferencing means the way to get back to the pointed value by the pointer. The pointer is declared by using the asterisk operator “*” and to save the address in the pointer variable, we must use the “&” operator after pointer declaration. Let us look at an illustration of declaring a pointer, “ int *ptr = &a”. As you can see, the pointer named “ptr” is referenced about the address of an integer “a” present in the class in the above example. So, adding another variable that will be like “c=*b” will mean that the c variable is dereferencing the address stored at b of the value a. The “&” operator is used for referencing a pointer, and the “*” operator is used for dereferencing a pointer. We can also use a pointer to point at a pointer in C++ by adding another “*” operator to the pointer like “**ptr_A”.
Initializing a dereference pointer:
As we know the basic concept of pointers and their types like reference and dereference pointers, we can move towards initialising them in C++. We know that the “*” operator is used for pointer initialization, and the “&” operator is used for storing addresses in the pointer variable. To initialize a pointer in the C++ programming language, the syntax is “int * ptr_A;”. Let us look at syntax more closely, the first part represents the data type of the pointer second part describes the nature of the variable, that is, in this case, a pointer, and the third and last part is the actual name of the pointer that we are saving it as. It is critical to assign a data type to the pointer variable; otherwise, it will not refer to an address because it does not point to any data type in the first place.
But till now, the pointer is empty, so to fill it in, we must include an address of a variable in its memory. So, if we are using “ptr_A” we can add an address of a variable by writing this command in the code “ptr_A = &a;”. This means that now the pointer variable “ptr_A” holds the address of a variable named as “a” as the “&” operator points to the address of a variable and is used for referencing pointer with addresses of the member functions of the current class.
Now we will implement a very simple logic for further explanation of referencing and dereferencing with pointers in the C++ in the Ubuntu 20.04 environment.
Implementing a simple dereference pointer in C++ in Ubuntu 20.04:
So, the executable file in ubuntu for a C++ program is a file with “.cpp”, so to create a.cpp file, open the terminal and type “cd Desktop” to get to the desktop directory, then “touch” with the filename and extension of “.cpp”. Then find and open your “.cpp” file on your desktop. Now we will write a code in that file in which we will simply initialize a pointer and a variable and then store the variable’s address in the pointer and dereference it to the pointer and display the result.
After that, the “.cpp” file should be saved and closed. Reopen the terminal and use the command “g++” with your file name and extension to build the file. This will generate an output file with the extension “.out” on your desktop for our “.cpp” file when it has been compiled. Now run the output file by typing “./” followed by the name of the output file.
After looking at the output, we can clearly understand the pointer “ptr” working. First, we initialized the pointer by defining its data type and then stored an address of a variable “num” in it. Then we printed the pointer in two different ways it displayed two different results; in the first output line, we saw the address of the variable “num” as it was the attribute of the pointer, but when we wrote the pointer name with a “*” operator before its name, it displayed us the actual value of the variable “num” which address was stored in the pointer. This is the concept of dereferencing pointer with which you refer to the actual value of the variable by only its address.
Now that we have a pretty good understanding of the working of a pointer, it is time to look into some further use of pointers in the C++ programming language.
Passing values to variables by using a deference pointer in C++ in Ubuntu 20.04:
As we know in the Ubuntu environment, the C++ program file is saved with the extension of “.cpp” so to create this file on our desktop, we will get to our terminal and write in the command line the “cd Desktop” command and then press enter and write the command “touch filename.cpp” to create a file with the “.cpp” extension. In that file, we will initialize a pointer and a variable and then store the variable’s address in the pointer, dereference it to the pointer, and then by using the dereferenced pointer, we will pass a value to the variable.
Get back to the terminal and compile the file with this command “g++” along with your file name and the “.cpp” extension. This command will generate an output file with an “.out” extension. Now run that file by writing this command “./” along with your “.out” extension.
As you can see, we successfully passed a value to the variable with the help of a dereferenced pointer. We simply called the dereferenced pointer, which had a reference that is the address of the variable and gave it a new value which then transferred to the variable itself.
In this article, we discussed about the concept of pointers and their types in the C++ programming language. We looked as to what a dereference pointer is and why we use them in the C++ programming language. Pointers itself is a very vast topic to look into the C++ programming as it helps the development by providing ease and excellence in the whole process. In this article, the usage of the dereference pointer is fully described, and the relevance of its use is demonstrated by an example in run-time circumstances in Ubuntu 20.04 environment in extensive detail. This will help you use the dereference pointer in the C++ programming language in Ubuntu 20.04.