0% found this document useful (0 votes)
25 views1 page

2 - Understanding Reference Variables

A reference in programming is an alias for an existing variable of the same data type, requiring no additional memory allocation. It is declared using a specific syntax that includes the data type, an ampersand, and the reference variable name assigned to a previously defined variable. The example provided illustrates how changes to the reference variable affect the original variable, as both point to the same memory location.

Uploaded by

Sarat Chandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views1 page

2 - Understanding Reference Variables

A reference in programming is an alias for an existing variable of the same data type, requiring no additional memory allocation. It is declared using a specific syntax that includes the data type, an ampersand, and the reference variable name assigned to a previously defined variable. The example provided illustrates how changes to the reference variable affect the original variable, as both point to the same memory location.

Uploaded by

Sarat Chandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

A reference is an alternative name for an object.

A reference variable provides an alias name for a previously defined variable of


the same data type. No extra memory is allocated to the reference variable.

A reference declaration consists of a data_type, an ampersand (&), a reference


variable name equated to a variable name which is previously defined.

The general form of declaring a reference variable is:

data_type &ref_var = variable_name;

Where data_type is any valid C++ data type, ref_var is the name of the reference
variable that will point to variable denoted by variable_name.

Let us consider an example:


int total;
int &sum = total;
sum = 100;
cout << total; // It prints 100

In the above code both variables total and sum refer to the same data object in the
memory, thus it prints the same value.

You might also like