Reference variables and memory address of variables in C++

Overview

In C++, a reference variable is a variable that is used to refer to an existing variable. This type of variable is created using the & operator.

Creating a reference variable

We use the & operator to create a reference variable.

For example:

string name = "Theo"; // this is a variable
string &myname = name; // this is the reference to the variable `name`

In the code written below, we will create a variable and a reference variable to refer to the initial variable.

Code example

#include <iostream>
#include <string>
using namespace std;
int main() {
string name = "Theo"; // this is a variable
string &my_name = name; // this is the reference to the variable `name`;
my_name = "Leo";
cout << name << "\n";
cout << my_name << "\n";
return 0;
}

Code explanation

  • Line 6: We create a string variable called name.
  • Line 7: We create a reference variable called my_name. This variable is used to refer to the name variable.
  • Line 9: We change the value of the reference variable my_name to Leo.
  • Line 11: We print the variable called name.
  • Line 12: We print the reference variable called my_name.

Applications of the reference variable

  1. The reference variable is used to modify the function’s parameters.
#include<iostream>
using namespace std;
void swap (int& number_one, int& number_two)
{
int temp = number_one;
number_one = number_two;
number_two = temp;
}
int main()
{
int first_number = 1, second_number = 2;
swap(first_number, second_number);
cout << first_number << " " << second_number;
return 0;
}

Code explanation

In the code written above, we swapped the values of the variables first_number and second_number by using the reference variable.

  1. The reference variable can modify all the objects in the for loop.

Code example

#include <bits/stdc++.h>
using namespace std;
int main()
{
// creating a vector object
vector<int> my_vector{ 10, 20, 30, 40 };
// modifying the elements of the vector
// using a reference variable
for (int &element : my_vector)
{
element += 5;
}
for (int element : my_vector)
{
cout << element << " ";
}
return 0;
}

Code explanation

In the code written above, we modified all the elements of the variable my_vector using the reference variable.

Getting the memory address of a variable using the “&” operator in C++

In C++, a memory address is the location on the computer where a variable is stored. A memory address is assigned to a variable when it is created. Also, whenever a value is assigned to the variable, it is stored in the memory address.

Getting the memory address of a variable

We use the & operator to get the memory address of a variable:

Note: It is worth noting that the memory address of a variable is represented in hexadecimal form. Also, the result in the code example given below may not be the same as the one in our program.

Code example

In the code given below, we will create a variable and use the & operator to obtain the memory address of the variable, that is, the location at which the variable is stored on the computer.

#include <iostream>
#include <string>
using namespace std;
int main()
{
// creating a string
string name = "Theo";
// obtaining the memory adress
cout << &name;
return 0;
}

Code explanation

  • Line 7: We create a string variable called name.
  • Line 10: We obtain and print the memory address of the string variable, using the & operator right after the variable name.

Getting the memory address of the variable and the reference variable

Now, we will determine the memory address of a reference variable using a code from before.

Code example

#include <iostream>
#include <string>
using namespace std;
int main()
{
string name = "Theo"; // this is a variable
string &my_name = name; // this is the reference to the variable `name`;
// obtaining the memory adress of the variable
cout << "Memory adress of the variable name: " << &name << endl;
// obtaining the memory adress of the reference variable
cout << "Memory adress of the reference variable: " << &my_name << endl;
return 0;
}

Free Resources