Reference Operator in C++
This article will focus on the reference operator &
, also called the address of
operator.
Although the reference operator works similarly in multiple cases, it has few differences across various programming languages. We will focus on its uses in the C++ programing language only.
First, we will learn its basic syntax and concepts. Then, we will answer the common confusion regarding the use of reference operators in different contexts.
Use the Reference Operator in C++
Syntax to declare a reference variable:
int &ref = m;
int & ref = m;
int& ref = m;
The &
operator has different uses and meanings according to the context in which it is used. The following section discusses the tasks the reference operator performs in C++.
Example:
// Simple integer type variable intialization
int m = 12;
// ref is a reference to m
int& ref = m;
// Print value of m
cout << "Original m = " << m << endl;
ref = 20;
// Print value of ref
cout << "Modified m = " << m << endl;
In the above code, we stored 12 in the variable m
. Next, we pointed the ref
to the m
using the reference operator.
Here, pointing means that ref
has the memory address of the m
variable. So, when we update the value of m
, we get the same updated value through the ref
variable, which is the reference variable.
In short, the ref
variable became an alias to another variable, m
. We can also say that ref
is just another name for the actual variable named by m
.
We assigned 20 to ref
in the above code snippet. This change will also be visible on m
having an updated value of 20.
Output:
Original m = 12
Modified m = 20
Compare the Reference Variable and Pointer Variable in C++
Usually, learners confuse the reference variable with the pointer variable.
In the previous section, we discussed the reference variable in detail with examples.
To define, a pointer variable stores the address of a variable in the memory. It uses the reference operator to initialize or define its values.
See the code below to understand the use of &
with a pointer variable; then, we will discuss the differences.
// Integer type variable intialization
int number = 88;
// Declare a pointer variable
int* ptrNumber;
// Assign the address of the "number" to "ptrNumnber"
ptrNumber = &number;
// Declare another int pointer and initialize it with the address of the number
int* pAnother = &number;
In this code, we first declared a pointer variable ptrNumber
and then assigned the address of number
in the third statement. Here, &
serves as the address of
operator and returns the memory address of number
, which is then assigned to the pointer ptrNumber
.
The last statement in the above snippet assigns the address of number
to another pointer, pAnother
at declaration. It is important to note that pointer variables are different from reference variables.
Let’s have a look at the differences using the table given below.
Pointer variable | Reference variable |
---|---|
Pointer keeps the memory address of a variable. | Reference variable is an alias for another variable. |
An indirection operator * is used to dereference a pointer. |
Reference variable is a constant pointer, which does not need dereferencing. |
It can be reassigned to refer to a variety of items. | It must be assigned at the time of initialization, and the address values cannot be changed once they have been formed. |
A NULL value can be directly assigned to a pointer variable. | NULL value cannot be set directly. |
The &
operator, when used along with formal parameters in a function, makes them reference parameters. More information about reference and value parameters here.
Husnain is a professional Software Engineer and a researcher who loves to learn, build, write, and teach. Having worked various jobs in the IT industry, he especially enjoys finding ways to express complex ideas in simple ways through his content. In his free time, Husnain unwinds by thinking about tech fiction to solve problems around him.
LinkedIn