Open In App

Pointers and References in C++

In C++ pointers and references both are mechanisms used to deal with memory, memory address, and data in a program. Pointers are used to store the memory address of another variable whereas references are used to create an alias for an already existing variable.

Pointers in C++

Pointers in C++ are a symbolic representation of addresses. They enable programs to simulate call-by-reference and create and manipulate dynamic data structures. Pointers store the address of variables or a memory location. 



Syntax

datatype *var_name; 

for example, int *ptr; //ptr points to an address that holds int data.

Example of Pointers in C++

The below program demonstrates the use of pointers in C++.






// C++ program to demonstrate use of pointers in C++;
 
#include <iostream>
using namespace std;
 
int main()
{
    int x = 10; // variable declared
    int* myptr; // pointer variable
 
    // storing address of x in pointer myptr
    myptr = &x;
 
    cout << "Value of x is: ";
    cout << x << endl;
 
    // print the address stored in myptr pointer variable
    cout << "Address stored in myptr is: ";
    cout << myptr << endl;
 
    // printing value of x using pointer myptr
    cout << "Value of x using *myptr is: ";
    cout << *myptr << endl;
 
    return 0;
}

Output
Value of x is: 10
Address stored in myptr is: 0x7ffd2b32c7f4
Value of x using *myptr is: 10

Explanation: The above program declares an integer variable ‘x’ initialized with value 10 and a pointer variable named ‘myptr’. The memory address of x is assigned to myptr. Then it prints the value of x, the address stored in myptr, and the value of x obtained by dereferencing the pointer myptr.

To know more about Pointers, how it works and its mathematical background refer to Pointers in C++.

Application of Pointers in C++

Following are the Applications of Pointers in C++:

Features and Use of Pointers in C++

The Pointers have a few important features and uses like it saves memory space, they are used to allocate memory dynamically, it is used for file handling, etc. Pointers store the address of variables or a memory location. 
Example: pointer “ptr” holds the address of an integer variable or holds the address of memory whose value(s) can be accessed as integer values through “ptr”.

int *ptr;

‘this’ Pointer in C++

The ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions. ‘this’ pointer is not available in static member functions as static member functions can be called without any object (with class name). Even if only one member of each function exists which is used by multiple objects, the compiler supplies an implicit pointer along with the names of the functions as ‘this’. 
Declaration: 

this->x = x; 

References in C++

When a variable is declared as a reference, it becomes an alternative name for an existing variable. A variable can be declared as a reference by putting ‘&’ in the declaration. There are 3 ways to pass C++ arguments to a function: 

Example of References in C++

The below program demonstrates the use of references in C++.




// C++ Program to demonstrate use of references
 
#include <iostream>
using namespace std;
 
int main()
{
    int y = 10;
 
    // ref is a reference to x.
    int& myref = y;
 
    // changing value of y to 20
    y = 30;
    cout << "value of y is " << y << endl;
    cout << "value of myref after change in value of y is: "
         << myref << '\n';
 
    return 0;
}

Output
value of y is 30
value of myref after change in value of y is: 30

Explanation: The above program demonstrates the use of references. First, it declares an integer variable ‘y’ and then creates reference ‘myref’ which is an alias to y. Changing the value of y also changes the value of myref which can be seen after printing values of both y and myref.

Pointers vs References

Pointers vs References in C++: This article lays the proper ground for differences between Pointer and references. Both references and pointers can be used to change the local variables of one function inside another function. Both of them can also be used to save copying of big objects when passed as arguments to functions or returned from functions, to get efficiency gain. 
Despite the above similarities, there are the following differences between references and pointers. 

Note A pointer can be declared as void but a reference can never be void.For example:

int a = 10;

void*aa = &a;. //it is valid

void &ar = a; // it is not valid


Article Tags :
C++