Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading

Reference Variables

Data Structures using C ++

The reference variables are used to pass the parameter to the functions.

Reference Variables

•  Definition : Reference variable is simply another name for already existing variable.

 

Difference between Reference and Pointer Variable


Reference

1. References must be initialized when created.

2. Once reference is assigned with some object it can not be changed.

3. One can not have NULL references.

Pointer

1. Pointer can be initialized at any time.

2. Pointers can point to another object at any time.

3. The pointer can be assigned with the value n be assigned with the NULL.


Creating Reference Variable

The reference variables are created using the &. For example we can create a reference variable x for an integer variable i. It is as follows

int i=10;

int &x=i;

Here variable x acts as a reference variable for variable i. Hence if value of i is changed then the value of x changes automatically. This is because variable x is a reference of variable i.

Here is illustrating program ‒

#include<iostream>

using namespace std;

void main()

{

       int i=10;

       int &a=i;

       cout<<"\n i= "<<i;

       cout<<"\n a= "<<a;

       i=100;

       cout<<"\n i= "<<i;

       cout<<"\n a= "<<a;

}

Output

i= 10

a= 10

i= 100

a= 100

 

Use of Reference

The reference variables are used to pass the parameter to the functions.

 

Points to Remember

1. Do not initialize reference variable with a constant value. For instance 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.

 

Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading : Tag: : Data Structures using C ++ - Reference Variables


Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading



Under Subject


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Electron Devices

EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Circuits and Network Analysis

EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Engineering Drawing - Laboratory

ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus - Laboratory

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Devices and Circuits Laboratory

EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation