Pointers: A pointer is a variable that holds memory address of another variable. A pointer needs to be de referenced with * operator to access the memory location it points to.
References: A Reference can be called as a constant pointer that becomes de referenced implicitly. When we access the reference it means we are accessing the storage.
Why do we need reference variables if we have pointers?
In Pointers to access the value of the actual variable, we need to explicitly deference the pointer variable by using ‘value at address’ dereferencing operator(*).
In References to access the value of the actual variable, we do not need to explicitly dereference the reference variable, they get de-referenced automatically.
Pointers and References are equivalent, except:
- A reference is like a constant name for an address. We need to initialize the reference during the declaration. Once a reference is established to a variable, we cannot change the reference to reference another variable.
- To get the value pointed to by a pointer, we need to use the dereferencing operator(*).
For Example: If a is a pointer to integer type, *a returns the value pointed to by a.
To assign an address of a variable b into a pointer, we need to use the address-of operator(&).
For Example: int *a= &b.
- Reference are used over pointer to avoid Object Slicing.
Object slicing happens when a derived class object is assigned to a base class object, additional attributes of a derived class object are sliced off to form the base class object.
- In references, referencing and de-referencing are done implicitly. No explicit de-referencing operator(*) and to assign the address of a variable to reference variable, no address-of operator(&).
- A reference variable provides a new name to the existing variable. It is de-referenced implicitly and does not need a de-referencing operator(*) to retrieve the value referenced. Whereas, to retrieve the value pointed to by a pointer we need de-referencing operator(*) which is known as explicit de-referencing.
- Reference can be treated as a constant pointer. It has to be initialized during declaration and its content cannot be changed.
Below is the program for the illustration of pointer and references:
20 26 23 26 18
Explanation of output:
- when pointer ptr pointed to num1 var then it printed 20
- when *ptr was de-referred and changed the value then it printed 26
- similar for another variable called num2, printed 23
- when reference ref1 was initialized by num1 then it printed 26 not 20
- finally when 18 was assigned into ref1 then it printed 18 due to implicit de-referencing
Illustration of Reference Variable:
Best example of the reference variable is the concept of copy constructor. Copy constructor takes a reference variable as an argument, pointer cannot be used here.
a = 5 b = 10
Explanation: In the above example if we take pointer in the argument of copy constructor then object of complex class will be created again and again which will never be stopped and it is error in oops concept. choosing reference is only the solution in this condition.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Output of the program | Dereference, Reference, Dereference, Reference....
- How to pass PHP Variables by reference ?
- Why C treats array parameters as pointers?
- How many levels of pointers can we have in C/C++
- When do we need Interfaces in PHP?
- Is there any need of "long" data type in C and C++?
- What are Wild Pointers? How can we avoid?
- Declare a C/C++ function returning pointer to array of integer pointers
- Introduction of Smart Pointers in C++ and It's Types
- Dangling, Void , Null and Wild Pointers
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- Pointers in C/C++ with Examples
- Pointers vs References in C++
- What are near, far and huge pointers?
- Computing index using pointers returned by STL functions in C++
- Program to reverse an array using pointers
- The length of a string using pointers
- Output of C programs | Set 64 (Pointers)
- Applications of Pointers in C/C++
- C++ Program to compare two string using pointers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.