C++

C++ Pointers and Reference

In C++, both the pointer and the reference are two powerful concepts. With the help of pointer and reference variable, we can easily access our normal variables easily. Now, we discuss in details about the concept of pointer and reference.

Pointer

Pointer is a special type of variable. It stores the address of another variable. It means whenever any variable store in memory, it gets a specific address. Pointer stores this address of that particular variable.

Programming Example 1

#include<iostream>
using namespace std ;

int main()
{
   int x =15 ;
    int *p ;       //Declaring a Pointer
    p = &x ;
    cout<< &x << endl ;  // address of x ;
    cout<< p << endl ;    // pointing the address of x ;
    cout<< *p << endl ;   // Dereferencing of a pointer;
     return 0 ;
}

Output

Explanation
Here, we declare an integer variable x and inside x we assign 15. Now, we declared an integer type pointer variable *p.

p = &x;

Here inside the pointer variable p, we assign the address of x. The address of x is totally depends on our operating system.

If we print &x, it shows the output of the address of variable x.

If we print p variable, it also gets the output of the address of variable x.

But if we print the *p, it actually dereferences to our pointer. It means it gets the output of the value of x variable.

Programming Example 2

#include<iostream>
using namespace std ;

int main()
{
   float x =15 ;
    float *p ;       //Declaring a Pointer
    p = &x ;
    cout<< &x << endl ;  // address of x ;
    cout<< p << endl ;    // pointing the address of x ;
    cout<< *p << endl ;   // Dereferencing of a pointer;
     return 0 ;
}

Output

Explanation
If we take the float type variable x and assign some value 1.5, then we declare a pointer to hold the address of x, we have to take float type of pointer.

Any pointer holds the address of variable; both have the same data type. Otherwise, error occurs.

Pointer Arithmetic

In C++ with the help of pointer, we have done some arithmetic operations like increment, decrement, addition and subtraction.

Programming Example 3

#include<iostream>
using namespace std ;
int main()
{
    int x =15 ;
    int  *p = &x ;
    cout<< p << endl ;  // address of x ;
     p++ ;  // Increment of poiner
    cout<< p << endl ;    
     return 0 ;
}

Output

Pointer and Array

Array always takes memory in a contiguous fashion. We can implement an array with the help of pointer. Because when pointer is incremented, it always points to next block from the base address of the array. Both pointer and array have the same data type.

Programming Example 4

#include<iostream>
using namespace std ;
 int main()
  {
    int i ;
     int arr[] = { 5 , 10 , 15 } ;  //Declaring an array;
     int *p = arr ;  // Initializing the value of array to a pointer;
     cout<< *arr << endl ;
        for ( i =0 ; i < 3 ; i++)
      {
          cout<< *p << endl ;
            }
      return 0;
}

Output

Explanation
We can access an array to a pointer. In this example, we simply declare an array and initialized some values. The base address of the array to a pointer p. Now, we can print the value of each element of the array through a pointer. If we increment the value of ptr, it simply goes to next block from the base address of the array.

Reference

Except normal variable and pointer, C++ provides us a special type of variable called reference variable. Now, we learn about the reference variable.

Features of Reference Variable

  1. Before declaring a reference variable, we simply write ‘&’ symbol. Many of us has misconception that ‘&’ is treated as address of operator. But actually it is not treated like that in terms of reference variable.
  2. When we declare the reference variable at that time, initialization have to be done. Otherwise, error occurs.
  3. Reference variable cannot be updated.

Example of a Reference Variable

int x = 5 ;

If we want to keep an alternate name of variable x, we have to follow this procedure.

int &y = a ;

It means reference variable is created only to refer the existing variable in the program. With the help of reference variable, we access the variable x.

Programming Example 5

#include<iostream>
using namespace std ;
int main()
{
    int x =15 ;
    int &y = x ;
    cout<< x << “ “ <<  y << endl ;
     ++y ;
    cout<< x << “ “ << y  << endl ;
   return 0 ;
}

Output

Explanation
Here, we introduce a reference variable y which refers to the variable x by the line &y = x. In x, 5 is assigned. If we want to print the variable x and y, both shows same result 5.

If we increment 1 to the value of y, and print both the variable x and y, it shows same result 6.

Conclusion

Discussing in detail about the concept of both pointer and reference, we have come to this conclusion that address is the most powerful concept in C++. With the help of pointer and reference, we can monitor the variables, array, function, structure etc. easily.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com