Example 1:
Let’s begin our first C++ code by adding the header file. The “iostream” is the included header file here. This makes it easy for us to utilize the functions like “cin” or “cout” in our code since these functions are declared in this header file. We also add the “std” namespace here and then call the “main()” method.
In the following, we create a pointer variable of “p” by placing “*” with the “p” variable and also initialize an “x” variable with the value of “10”. Both are the “int” data type. Then, we assign the address of the “x” variable to “p”. After this, the “cout” statement is placed which renders the address of “x” since we added “p” here. Then, we apply the dereferencing and access the value of “x” which is stored in the “p” pointer. We also use the “cout” to print the value that we get by dereferencing the pointer.
Code 1:
using namespace std;
int main()
{
int *p, x = 10;
p = &x;
cout << "The address of x is " << p << endl;
cout << "The value of x by dereferencing pointer is " << *p << endl;
return 0;
}
Output:
The address of the “x” value is displayed first. Then, the value of “x” is displayed which we get by utilizing the “*p” pointer or by dereferencing the pointer.
Example 2:
We apply the dereferencing technique on the float data type variable here. So, we call the “main()”after adding the “iostream” header file as well as the “std” namespace. Below this, we initialize “f_1″ which is the variable name of the “float” data type. We assign “4.987” to the “f_1” variable. Then, we initialize the “f_ptr” pointer which is the pointer of the float data type. We save the address of the “f_1” variable in the “f_ptr” pointer by utilizing the “&” symbol.
After this, we use “cout” and place “f_ptr” in it which returns the address of “f_1”. Then, we apply the dereferencing in which we utilize “*f_ptr” to get the value which is saved in the “f_1” variable.
Code 2:
using namespace std;
int main() {
float f_1 = 4.987;
float* f_ptr = &f_1;
cout << "Address of f_1: " << f_ptr << endl;
cout << "Value of f_1: " << *f_ptr << endl;
return 0;
}
Output:
The value of “f_1” which we obtain by dereferencing the pointer with the help of using “*f_ptr” is displayed after the address of the “f_1” value is shown.
Example 3:
Now, we perform the dereferencing technique on the “string” type data. After invoking the “main()” in this code, we place the “string” data type and initialize a “newName” variable. We save the name “Jack” in this “newName” variable. We also initialize a pointer below with the name “name_ptr” and assign the address of the “newName” variable to this pointer.
Now, we want to print the pointer’s address and the value that this pointer points to. The “name_ptr” contains the address of the value where the pointer points, and the “*name_ptr” is utilized here as a dereferencing pointer. Then, it returns the value of the variable where the “name_ptr” points. We place both of them inside the “cout” so they render as the outcome.
Code 3:
#include <string>
using namespace std;
int main() {
string newName = "Jack";
string* name_ptr = &newName;
cout << "The address of the name is: " << name_ptr << endl;
cout << "The name by dereferencing: "<< *name_ptr << endl;
return 0;
}
Output:
The address where we stored the name is printed here. Then, the name itself is displayed which is the string that we stored in the variable. We get it here using the dereferencing pointer technique.
Example 4:
Now, we change the pointer’s value where that pointer points and see that it will not change the variable’s value. The pointer is initialized first with the name “my_pntr” by putting the “*” symbol. The “val_1” and “val_2” are initialized with the values of “56” and “21”, respectively. Their data type is “int”. The “val_1” address is assigned to “my_pntr”. Then, we display the address as well as the value by utilizing the “cout” and placing “my_pntr” and “*my_pntr” in it. The “my_ptr” returns the address, and the “*my_ptr” returns the variable’s value where this pointer points.
Now, we reference the address of the same pointer which is “my_pntr” and assign the address of “val_2” to the “my_pntr” pointer. Now, this pointer points to another variable address, and the value of the previous variable is still the same. Now, we print the address of the new variable and also the value of that pointer by placing “my_pntr” and then dereferencing to the pointer with the help of “*my_pntr”. After this, we also print the value of “val_1”.
Code 4:
using namespace std;
int main()
{
int *my_pntr;
int val_1 = 56;
int val_2 = 21;
my_pntr = &val_1;
cout << "The address of val_1 is " << my_pntr << endl;
cout << "The value of val_1 is " << *my_pntr << endl;
my_pntr = &val_2;
cout << "Now, we changed the position of the pointer, which is " << my_pntr << endl;
cout << "The new value is of the pointer is " << *my_pntr << endl;
cout << "The value of val_1 is still " << val_1;
return 0;
}
Output:
The address of “val_1” and the value of “val_1” are displayed first. Then, we change the position of the pointer, the new value, and the new variable’s address. After this, the “val_1” value is displayed. We might notice that after changing the pointer’s position where the pointer points, the first variable’s value is not affected; the pointer just moves to another location.
Example 5:
The “n_1” and “n_2” are initialized with “33” and “98” as the “int” variables. Then, the pointer is created here as “*pointer”. After this, we place the “n_1” address to this “pointer”. So, this pointer is now pointing to the value of the “n_1” variable.
Next, we use the “cout” function to display both the address and the value by inserting “pointer” and “*pointer” into it. The address is returned by the “pointer”, and the value of the variable to which this pointer points is returned by the “*pointer” as dereferencing the pointer. We now assign the address of “n_2” to this pointer by referencing the same pointer which is “pointer”. This pointer now goes to a different variable location while the value of the preceding variable remains unchanged.
Using “cout”, inserting “pointer”, and using “*pointer” to denote the value of the variable, we are now able to output both the address of the new variable and its value.
Code 5:
using namespace std;
int main()
{
int n_1 = 33;
int n_2 = 98;
int* pointer;
pointer = &n_1;
cout << "The address of n_1 is: " << pointer
<< endl;
cout << "The value of n_1 by dereferencing the pointer is: "
<< *pointer << endl;
pointer = &n_2;
cout << "The changed position of the pointer is: " << pointer << endl;
cout << "The new value of the same pointer is: "
<< *pointer << endl;
return 0;
}
Output:
First, the address and value of “n_1” are shown. Next, the pointer’s position is adjusted, and the new value and address of the new variable are shown in the following. Referencing the address of the pointer and then dereferencing the pointer for displaying the value is clearly shown in this example.
Conclusion
The dereference pointer in C++ is thoroughly examined in this article. We explored that dereferencing is the technique that is used to get the value of the variable where the pointer points. We explored five different examples in which we utilized the dereference pointer in C++ programming. We also showed the outcome of all the examples here along with the codes.