On the other hand, a pointer to const means that the value that the pointer indicates is immutable and constant. Nevertheless, the pointer itself may move and point to a different location. Now, we will explore both concepts in this tutorial.
Example 1: Const Pointer
Here is an example of the const pointer in which we will show how a const pointer acts in C++ programming. To start our C++ code, we must add the header files that we need in our code. We put the “<iostream>” header file along with the “#include” keyword. It is utilized for the input/output functions because these are declared in this header file.
After this, we write “using namespace std” in which “std” is the short form of standard. So, this line of code describes that we utilize the whole thing in our code with the “std” namespace. Then, we initialize the “main()” function as “int”. Inside this, we declare and initialize two variables named “num_1” and “num_2” with “40” and “80” values, respectively. Both have the integer data type.
After this, we have a “cout” statement which displays the message that is written on the screen. Below this, we create a constant pointer of the “pntr_1” name which is initialized with the address of “num_1”. Then, we utilize the “cout” again and place “*pntr_1” here. This displays the value where the pointer points. After this, we simply print the address of “pntr_1”. We then alter the value and set it to “63”. After this, we perform the same steps again. Now, when we execute this code, the pointer’s value will change but the address remains the same.
Code 1:
using namespace std;
int main()
{
int num_1{ 40 };
int num_2{ 80 };
cout << "Consant pointer codes in C++ " <<endl <<endl ;
int* const pntr_1{ &num_1 };
cout << *pntr_1 << endl;
cout << pntr_1 << endl;
*pntr_1 = 63;
cout << *pntr_1 << endl;
cout << pntr_1 << endl;
return 0;
}
Output:
After successfully compiling the given code, we get the following result. Here, we can see that the value changes from “40” to “63”, but the address is the same for both values:
Example 2:
Now, one more example of a const pointer is illustrated here. First, we add the “<iostream>” header file and then use “namespace std”. After this, we have the “main()” function in which we initialize an integer variable named “num_a” with a value of “45”. Below this, we create a constant pointer with the “pr” name and assign the address of the “num_a” variable to this. Now, we print that pointer address and the value by utilizing the “cout” statement. Then, we change the value of “pr” to “19” and print the “pr” again along with the value and address.
Code 2:
using namespace std;
int main ()
{
int num_a = 45;
int *const pr{ &num_a };
cout << "Address of pr: " << pr << " Value: " << *pr << endl;
*pr = 19;
cout << "Now the value of pr is 19 !! " << endl
<< "Address of pr: " << pr << " Value: " << *pr << endl << endl;
return 0;
}
Output:
We now obtain the subsequent outcome. As we can see, the value of “pr” in this case shifts from “45” to “19”, but the address remains the same since it is the const pointer.
Example 3: Pointer to Const
Now, we have an example of a pointer to const in C++ programming. Here, we will see that the data or value of the pointer is constant until or unless we change the location where the pointer points. We start our code in the same way that we described in our previous codes. Inside the “main()”, we have the integer type variables of “a” and “b” which are initialized here with “96” and “105”, respectively. After this, we display the value of both variables with the aid of “cout”. Then, we place the “alpha” pointer and assign the “a” address to this pointer. Then, we print this “alpha”. Now, we alter the address where the pointer points, set it to “b” from “a”, and print it here again.
Code 3:
using namespace std;
int main()
{
int a{ 96 };
int b{ 105 };
cout << "The value of a is " << a <<endl;
cout << "The value of b is " << b << endl;
const int* alpha{ &a };
cout << "The alpha is a now " << *alpha << endl;
alpha = &b;
cout << "Now we change alpha to b which is " << *alpha << endl;
cout << "Now we change alpha to b which is " << *alpha << endl;
return 0;
}
Output:
Here is the outcome of the code. The value of alpha is changed. First, the value of alpha is the value of the “a” variable. But in the end, the alpha’s value changed. This is because we changed the alpha point location and set it to “&b”.
Example 4:
In this code, we have three integer variables with the names “var_1”, “var_2”, and “var_3” which are initialized by the values of “8”, “12”, and “14”, respectively. Now, we display all variables separately here. Then, we have “const int *ptr”, the pointer named “ptr”. We assign the location of “var_1” to this “ptr” and then print it here.
After this, we change the location of the point where “ptr” points and set it to “var_2”. Then, we print the updated “ptr” again. Now, we place another pointer named “ptr_2” and assign the location of “var_3” to this pointer. We also print it here along with the address. Then, we change the value of “var_3” with the aid of the pointer. Now, we set it to “32”. After this, we print the updated value of “ptr_2” and the address of the pointer.
Code 4:
using namespace std;
int main ()
{
int var_1 = 8, var_2 = 12, var_3 = 14;
cout << "var_1 = " << var_1 << endl;
cout << "var_2 = " << var_2 << endl;
cout << "var_3 = " << var_3 << endl;
const int *ptr;
ptr = &var_1;
cout << "Address of ptr: " << ptr << " Value: " << *ptr << endl;
ptr = &var_2;
cout << "ptr points to var_2 now!! " << endl;
cout << "Address of ptr: " << ptr << " Value: " << *ptr << endl;
int *const ptr_2 = &var_3;
cout << "Address of ptr_2: " << ptr_2 << " Value: " << *ptr_2 << endl;
*ptr_2 = 32;
cout << "ptr_2 holds value 32 now!! " << endl <<endl;
cout << "Address of ptr_2: " << ptr_2 << " Value: " << *ptr_2 << endl << endl;
return 0;
}
Output:
Here, we can see that the values are changed because we altered the location of the pointer points and displayed them on this output screen.
Conclusion
This tutorial thoroughly explained the difference between the const pointer and the pointer to const in C++ programming. We illustrated this with unique examples of C++ where we explained about the const pointer and the pointer to const. We also provided the output along with the codes in this tutorial.