Reference and Pointer
Prof. Tejashree V. Pawar
Cummins College of Engineering
Objective
⚫ Understand the references in C++.
⚫ Compare and contrast References and Pointers.
⚫ Return by references in C++.
Reference variable
⚫ A Reference is an alias / synonym for an existing variable.
⚫ Syntax:
⚫ data_type & reference_name = variable_name;
⚫ Eg,
⚫ int i=15; // i is a variable
⚫ int &j = i; // j is reference to i
i variable
15 Memory contents
300 address
j Reference or alias
Behavior of reference
A Reference variable must be initialized at the time of
declaration.
Here '&' is not an address operator.
int & means reference to int
Both variables refer to the same data
object in the memory
Reference variable
⚫ Examples,
1. int a[10];
2. int &x = a[10]; // x is alias/reference for a[10]
3. float b, &z=b; // float & means reference to float
4. int a;
5. int *p=&a;
6. int &x=*p;//x indirectly refer to a through pointer variable p.
7. int &n =50;//creates an int object with value 50 and name n. but
gives error.
Pitfalls in Reference
Pitfalls in reference
Wrong Reason Correct
declaration declaration
int &i; No variable refer to-- must be initialized int &i =j;
int &j = 6; No address to refer as 6 is constant const int &j = 6;
int &i = j+k; Only temporary address (j+k) to refer to const int &i = j+k;
Major application of reference variables is in passing arguments to functions.
Difference between use of
pointer and reference
Call by address
Program for swap call by reference
C++ Program using pointer C++ Program using reference
Return by Reference
⚫ A function can return a value by reference.
⚫ C uses return by value.
⚫ Technique of returning reference is used to establish cascade of member
functions calls in operator overloading.
Return by Reference
In function main(), the statement min(a,b)=500 calls the function min().
It returns reference to the variable containing minimum value and assigns
the value 500 to it.
The return type of function min() is int '&' (reference), it indicates that the
call to function min() can be written on the left side of assignment operator.
Consequently, the statement min(a,b)=500 is assign 500 to the variable
containing minimum value.
Return by Reference
Return by value Return by reference
const needed const optional
Return variable is temporary Return variable is an alias of 'a'
Has a different address Has the same address
Difference between Reference & pointers
Pointers References
Refers to an address Refers to an address
Pointers can point to NULL. References cannot be NULL
int *p =NULL; Int &j; // wrong
Pointers can point to different variables at For a reference, its referent is fixed
different times. int a,b,&p=a; //okay
int a,b,*p; ---
--- &p=b; // error
p=&a; // p points to a
---
p=&b; // p points to b
NULL checking is required Makes code faster
Does not required NULL checking
Allows users to operate on the address- diff Does not allow users to operate on the address.
pointers, increments, etc. All operations are interpreted for the referent.