Difference Between Reference Variable and Pointer Variable
A reference is the same object, just with a different name and a reference must refer to an object. Since references can’t be NULL, they are safer to use.
- A pointer can be re-assigned while a reference cannot, and must be assigned at initialization only.
- The pointer can be assigned NULL directly, whereas the reference cannot.
- Pointers can iterate over an array, we can use increment/decrement operators to go to the next/previous item that a pointer is pointing to.
- A pointer is a variable that holds a memory address. A reference has the same memory address as the item it references.
- A pointer to a class/struct uses ‘->’ (arrow operator) to access its members whereas a reference uses a ‘.’ (dot operator)
- A pointer needs to be dereferenced with * to access the memory location it points to, whereas a reference can be used directly.
Example: The following C++ program demonstrates the differences.
C++
// C++ program to demonstrate differences // between pointer and reference #include <iostream> using namespace std; struct demo { int a; }; int main() { int x = 5; int y = 6; demo d; int * p; p = &x; p = &y; // 1. Pointer reinitialization allowed int & r = x; // &r = y; // 1. Compile Error r = y; // 1. x value becomes 6 p = NULL; // &r = NULL; // 2. Compile Error // 3. Points to next memory location p++; // 3. x values becomes 7 r++; cout << &p << " " << &x << '\n' ; // 4. Different address cout << &r << " " << &x << '\n' ; // 4. Same address demo* q = &d; demo& qq = d; q->a = 8; // q.a = 8; // 5. Compile Error qq.a = 8; // qq->a = 8; // 5. Compile Error // 6. Prints the address cout << p << '\n' ; // 6. Print the value of x cout << r << '\n' ; return 0; } |
Output
0x7ffdfc7bead8 0x7ffdfc7bead4 0x7ffdfc7bead4 0x7ffdfc7bead4 0x4 7
Passing By Pointer vs Passing By Reference in C++
In C++, we can pass parameters to a function either by pointers or by reference. In both cases, we get the same result. So, what is the difference between Passing by Pointer and Passing by Reference in C++?
Let’s first understand what Passing by Pointer and Passing by Reference in C++ mean:
Contact Us