C++

What is a Const Pointer in C++?

The word “const” i.e., constant, means “no change” to any variable value within the programming languages. The pointer in programming refers to a variable that is pointing towards some specific address, probably of some variable address. The constant pointer will be one that will always point in the direction of the same address. This means the address of a variable to which the pointer is pointing cannot be updated so far. Therefore, in this article, we will see what is a constant pointer in C++ and how it works. So, let’s have a good start.

Example 01: Pointer

Let’s have our first example to demonstrate how a constant pointer works in the terminal shell. Start by opening the shell console application in your Linux distribution. You can use the “Ctrl+Alt+T” shortcut at Ubuntu 20.04 desktop to do so. Right after opening the shell, let’s have a creation of a new C++ file. So, the “touch” query will be used here so far. Mention the name of a file in the “touch” instruction that you want to create with the “.cc” extension.

After creating a file, you can easily open your file from the home folder of your Linux system. To edit and update it within the shell, you have to make use of some editor modules. If you don’t have one, try to install the “GNU nano” editor or Vim editor. As shown in the attached image below, we have been using the “nano” editor to open the newly made “const.cc” command in the shell.

$ touch const.cc
$ nano const.cc

The file will be opened empty in the GNU Nano editor. In our first example, we will first see the working of a simple pointer variable to understand constant pointers more. So, add the required libraries, i.e., standard input-output stream header and stdio header, by using the “include” keyword with a hash sign at the start. You have to define the standard namespace with the word “using”. After this, the main() method will be initiated as we have to do all work within it. Moreover, compilation and execution start from here. Initialize an integer variable “a” with an integer value assigned to it, i.e., 98.

The “cout” stream is used to display the value of variable “a”. After this, an integer type pointer “ptr” has been initialized, pointing towards the address of integer variable “a”. Due to this pointer, we can change the variable “a” as both are not constant right now. After this, the pointer “ptr” has been displayed, i.e., address of variable “a”. In the next line, we have used the increment operator to increment the value of the pointer “ptr” address value as it is pointing towards the address of variable “a”.

The pointer “ptr” has been displayed once again with the help of the “cout” stream. The next “cout” stream is used to display the variable “a” value once again, i.e., incremented one. The code ends here. Save the newly created code and leave the GNU Nano editor by using the “Ctrl+S” and “Ctrl+X” from the keyboard.

Let’s compile this newly made C++ code first. Use the “g++” compiler to do so. If you don’t have one already installed in your system, try to configure it first. After the compilation got successful, make your code run using the “./a.out” command. You will see, as the first “cout” statement got executed, it will display the value of a variable “a” i.e., 98.

Upon the execution of a second and third “cout” stream, it displayed the same address saved in the pointer “ptr” which is pointing towards the variable “a”. The increment has been executed on the value of a variable “a” through pointer “ptr”. Thus, upon the execution of a 4th “cout” statement, the increment value of a variable “a” has been shown on the terminal screen.

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

Example 02: Constant Pointer to Integer

This was all about the simple pointer pointing towards some variable address. Now, let’s have a look at the example of using a constant type pointer to point towards some variable. As we know, the word constant means “no change” when it is applied to some variable. So, we will use it as a pointer variable to see how a constant pointer will behave in certain situations. Thus, we have opened the same file with the “gnu nano” editor and updated its code a little.

The initialization line of a pointer has been updated with the word “const” along with the “*” sign at its start. Make sure to use it after the data type “int” within the initialization of a pointer “cptr”. Then, we have used the “cout” statement to increment the value of variable “a” as the pointer “cptr” is pointing towards it. The very next cout statement has been initialized to increment the pointer “cptr” itself. This will cause the compilation error as the “cptr” itself is constant. The rest of the code is left unchanged and saved using the “Ctrl+S”.

When we have compiled the code of the const.cc file, it gives us the error at line 10. As the pointer was constant, the error states that the “cptr” is read-only and can’t be incremented as expected.

$ g++ const.cc

When we have compiled the code, the variable “a” has been displayed with its original and updated value. While the address of the “cptr” pointer has been the same and not modified.

$ ./a.out

Example 03: Constant Pointer to Constant Integer

Within this example, we will be taking both pointer and integer it points to as constant. This means both cannot be updated. So, open the same file to make it up to date. We have initialized a constant type integer “b” with a value of 13. This variable has been displayed via the “cout” statement. Then, we have initialized a constant type pointer “cptrC” pointing towards the constant variable “b” with the “&” sign. The cout clause is used to display the pointer “cptrC”. After this, the constant variable “b” value will be incremented by the constant variable “cptrC”.

In the next consecutive line, the pointer “cptrC” itself has been incremented. Both the increment lines will show the error at compilation. The last two cout statements are utilized to display the value of constant variable value and constant pointer.

After compiling the code, we have got an error at both the increment lines, i.e., 9 and 10.

$ g++ const.cc

Thus, upon the code’s execution with the help of a “./a.out” instruction, we have got the old result of the last example, and the code has not been executed.

$ ./a.out

Conclusion:

Finally, we have done the working of constant variables in C++. We have discussed the examples of simple pointers, constant pointers to integers, and constant pointers to constant integers to enhance the level of understanding about pointers. We have used the “cout” statement, increment operators, and & operators to achieve this goal. We hope this article will be equally beneficial to new and already experienced users of C++ in the Ubuntu 20.04 system.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.