C++ References

Reference is used to operate on any existing variable's memory address inside C++ program:

Create Reference in C++

To create a reference variable in C++. Let's take a look at the following example. Here there is a definition of a simple variable.

int i = 17;

We can declare reference variables for i like this:

int& ir = i;

Read the & in these declarations as reference.

C++ References Example

Here is an example program demonstrating the use of references on int and double:

/* C++ References */

void main()
   int i;
   double d;
   int &ir = i;
   double &dr = d;
   cout<<"Enter any two number (integer and double) : ";
   cout<<"\nValue of i (simple) : "<<i<<"\n";
   cout << "Value of i (reference) : "<<ir<<"\n";
   cout<<"Value of d (simple) : "<<d<<"\n";
   cout<<"Value of d (reference) : "<<dr<<"\n";

Here is the sample run of the above C++ program:

c++ references

C++ Online Test

« Previous Tutorial Next Tutorial »

Like/Share Us on Facebook 😋