C++ Operations on Pointers

 

Operations on Pointers

Address of Operator (&):

& is also known as the Referencing Operator. It is a unary operator. The variable name used along with the Address of operator must be the name of an already defined variable.

 

Using & operator along with a variable gives the address number of the variable.

 

Here’s one example to demonstrate the use of the address of the operator.


#include <iostream>
using namespace std;
 
int main()
{
    int a = 10;
    cout << "Address of variable a is " << &a << endl;
    return 0;
}


Output:

0x61febc

 

 

Indirection Operator

* is also known as the Dereferencing Operator. It is a unary operator. It takes an address as its argument and returns the content/container whose address is its argument. 

 

Here’s one example to demonstrate the use of the indirection operator.


#include <iostream>
using namespace std;
 
int main()
{
    int a = 100;
    cout << "Value of variable a stored at address " << &a << " is " << (*(&a)) << endl;
    return 0;
}


Output:

Value of variable a stored at address 0x61febc is 100

  

Pointer to Pointer

Pointer to Pointer is a simple concept, in which we store the address of one pointer to another pointer. This is also known as multiple indirections owing to the operator’s name.  Here, the first pointer contains the address of the second pointer, which points to the address where the actual variable has its value stored. 

 

An example to demonstrate how we define a pointer to a pointer.


#include <iostream>
using namespace std;
 
int main()
{
    int a = 100;
    int *b = &a;
    int **c = &b;
    cout << "Value of variable a is " << a << endl;
    cout << "Address of variable a is " << b << endl;
    cout << "Address of pointer b is " << c << endl;
    return 0;
}


Output:

Value of variable a is 100
Address of variable a is 0x61feb8
Address of pointer b is 0x61feb4

  

Arrays and Pointers

Storing the address of an array into pointer is different from storing the address of a variable into the pointer. The name of an array itself is the address of the first index of an array. So, to use the (ampersand)& operator with the array name for assigning the address to a pointer is wrong. Instead, we used the array name itself.

 

An example program for storing the starting address of an array in the pointer,


int marks[] = {99, 100, 38};
int *p = marks;
cout << "The value of marks[0] is " << *p << endl;

Output:

The value of marks[0] is 99

 

In order to access other elements of the same array that pointer p points to, we can use pointer arithmetic, such as addition and subtraction of pointers.


*(p+1) returns the value at the second position in the array marks. Here’s how it works.

int marks[] = {99, 100, 38};
int *p = marks;
cout << "The value of marks[0] is " << *p << endl;
cout << "The value of marks[1] is " << *(p + 1) << endl;
cout << "The value of marks[2] is " << *(p + 2) << endl;


Output:

The value of marks[0] is 99
The value of marks[1] is 100
The value of marks[2] is 38