C and C++

Explain reference variable

A reference variable is just another name to an already existing variable.

Creating Reference Variable
The reference variable is created using the & symbol.

For example, let a is a variable and we can create a reference variable of a as x as follows,

int a =10;

int &x=a;

Here variable x is a reference variable for variable a. Both a and x are pointing at the same memory location. Hence if the value of a is changed then the value of x also changes automatically.

Note:

1. Reference variables should not be initilized with a constant value. For example int &a=100 is not allowed.

2. Never return the reference variable from a function as a memory address.

3. Avoid assigning the reference variables to the variables whose memory is dynamically allocated.

 

Leave a comment

Your email address will not be published. Required fields are marked *