In this C++ tutorial, you will learn how to access the value of variables pointed by the pointer variables using pointer concept discussed in detail.
It is possible to access the value of variables pointed by the pointer variables using pointer. This is performed by using the Dereference operator in C++ which has the notation *.
The general syntax of the Dereference operator is as follows:
*pointer_variable
In this example, pointer variable denotes the variable defined as pointer. The * placed before the pointer_variable denotes the value pointed by the pointer_variable.
For example:
exforsys = 100;
test = exforsys;
x = &exforsys;
y=*x;
In the above example, the assignment takes place as below:
That is
In the above example, the exforsys is a integer variable having the value of 100 stored in memory address location 3501.
The variable exforsys is assigned to the variable test in the second statement:
test = exforsys;
The value of the variable exforsys is 100 and is then copied to the variable test.
In the third statement, the address of the variable exforsys denoted by reference operator &exforsys is assigned to the variable x as:
x = &exforsys;
The address of the variable 3501 and not the contents of the variable exforsys is then copied into the variable x.
The fourth statement makes use of the deference operator:
y=*x
This means that the value pointed to by the pointer variable x gives the value 100 to y.
Some points for the programmer to note:
- The programmer must note that the x refers to the address 3501 whereas *x refers to the value stored in the address 3501 namely 100. .
- The reference operator is denoted by & and deference operator denoted by * . Both differ in their meaning and functionality. The reference operator denotes the address of. The dereference operator denotes the value pointed by. In short, a deference variable can be denoted as referenced. .
- If the programmer wants to define more than two pointer variables, then comma operator may be used in this instance. The programmer must carefully place pointer symbol * before each pointer variable.
For instance, if the user wishes to define two integer pointer variables, e1 and e2, this can be done as follows:
int *e1,*e2;
If the programmer declares:
int *e1,e2;
This means that e1 is a pointer variable pointing to integer data type but e2 is only an integer data type and not a pointer type. The programmer must ensure to place * before each pointer variable.
The dereferencing or indirect addressing is performed using the indirection operator * used to access the value stored in an address.
The defining of pointer variable:
int* exf;
The definition of pointer variable as in the above case is the pointer variable exf.
It is also possible to assign value to pointer variable using indirection operator. This gives the same effect as working with variables.
For Example:
#include <iostream>
using namespace std;
void main()
{
int example,test;
int* exforsys;
exforsys=&example;
example=200;
test=200;
*exforsys=100;
test=*exforsys;
cout << *exforsys << endl << test;
}
The output of the above program is
In the above example, the pointer variable exforsys points to integer variable example and takes the address of the variable example as:
test and example have initial values of 200.
The statement:
*exforsys=100
Sets the value of the variable pointed by pointer variable exforsys as 100. This is equivalent to setting example=100.
The assignment is as follows:
example test
The statement
test=*exforsys;
sets the value of variable test with the value pointed by the pointer variable exforsys which is 100 as seen in the above example. Test also becomes 100 and the assignment becomes:
Both results are displayed having the value 100.
Defining pointers and using them to access the variable pointed by them is an important concept thoroughly detailed in this tutorial. In the next section, the powerful concept of pointers with other features of C++ such as arrays and functions will be explained in detail.