
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pointers vs References in C++
In C++, both pointers and references are used to access and manipulate memory. But they behave differently. This guide explains each with simple words and examples.
We understand the topic by learning how each is declared, used, and what differences exist between them.
What are C++ Pointers?
The pointers are used to store the address of a variable. We can change what they pointing to, and also can assign NULL to them.
A pointer is similar to a signpost that contains the memory address of another variable, and you can directly access or change the variable by its address.
Syntax
The following is the syntax to declare and initialize pointers in C++:
type *pointerName; pointerName = variableName;
Let's go through the related examples for a better understanding of the pointer:
Example 1: Basic Pointer
In this example, we use a pointer to access and display both the value of a variable and its memory address.
#include<iostream> using namespace std; int main() { int x = 10; int *ptr = &x; cout<< "Value:"<<*ptr<<endl; cout<< "Address:"<<ptr<<endl; return 0; }
Output
The above program produces the following result:
Value:10 Address:0x7ffc079b213c
Example 2: Pointer Reassignment
Here, we point one variable and then reassign it to point to another variable.
#include<iostream> using namespace std; int main() { int a = 5, b = 15; int *ptr = &a; ptr = &b; cout<<"Pointer now points to:" <<*ptr; return 0; }
Output
The above program produces the following result:
Pointer now points to: 15
Example 3: Null Pointer
In this example, we check if a pointer is 'NULL' (not pointing to any memory location).
#include<iostream> using namespace std; int main() { int *p = NULL; if(p == NULL) cout<<"Pointer is null"<<endl; return 0; }
Output
The above program produces the following result:
Pointer is null
What are C++ References?
When a variable is defined as a reference, it is nothing but an alternative name for a variable that is already defined.
A reference is a nickname for a variable by which you can access and change the actual variable directly with the help of this nickname, rather than making a duplicate copy of it.
Syntax
Following is the syntax to the reference:
type &refName = originalVariable;
Let's go through the related examples for a better understanding of the reference:
Example 1: Reference Behavior
In this example, we use a reference to a variable that displays its value by modifying the value through the reference, resulting in the updated value of the original variable.
#include<iostream> using namespace std; int main() { int x = 100; int &ref = x; cout<<"Reference value: "<<ref<<endl; ref = 200; cout<<"Value of x: "<<x<<endl; return 0; }
Output
The above program produces the following result:
Reference value: 100 Value of x: 200
Example 2: Reference Must Be Initialized
In this example, we use a reference to a variable by assigning a new value to the original variable through the reference, and displays the updated value of the original variable.
#include<iostream> using namespace std; int main() { int a = 10; int b = 20; int &ref = a; // ref = b; does not change the reference itself ref = b; // changes the value of 'a' to 20 cout<<"Value of a: "<<a; return 0; }
Output
The above program produces the following result:
Value of a: 20
Example 3: Reference Alias
In this example, we use a reference to a variable to demonstrate how the changes made will reflect through the reference to the original variable.
#include<iostream> using namespace std; int main() { int x = 50; int &ref = x; cout<<"x = "<< x << ",ref = "<<ref<<endl; x = 60; cout<<"x = "<<x<<",ref = "<<ref<<endl; return 0; }
Output
The above program produces the following result:
x = 50, ref = 50 x = 60, ref = 60
Differences Between Pointers and References
The main differences between pointers and references are -
Pointer | Reference |
---|---|
Stores the address of a variable | Gives another name to an existing variable |
Can be null | Cannot be null |
Can be declared without initialization | Must be initialized when declared |
Has its own memory address | Shares memory with the original variable |
More complex (uses * and & ) |
Cleaner and simpler syntax |
Can be changed to point to another variable | Cannot be changed to refer to another variable |
Used in dynamic memory, data structures, etc. | Mainly used for function parameter passing (by reference) |
Needs dereferencing (*ptr ) to access value |
Direct access to value (no dereferencing) |