References in Cplus Plus
the reviewer(References)These are addresses that are given to anything (eg variables, arrays, and objects) that is defined in memory when the program is running.
The addresses of things that are allocated space in memory, are placed in a mannerHexadecimalWhere you will find most of the addresses contain numbers and letters as follows 0xd5ef87c
.
Accessing things in memory is very important as it makes you able to reduce the amount of memory your program needs.
It may also make the code size smaller, as you can access the things in it directly, and this is something you will notice if you are working on large projects.
Accessibility to things in memory is the most important characteristic of a languageC++As for other languages, such as Java and Python, this is not possible.
Now, to access the addresses of objects in memory we use the operator &
calledAddress Operator.
Print the addresses of things in memory inC++
To print the address of the space allocated to any variable that has been defined in memory, we put &
before its name, as we will see in the following example.
Example of printing addresses of objects in memory in C++
#include <iostream> using namespace std; int main() { // and its value is 5 x here we have defined a variable named int x = 5; // In memory x here we have printed the address of the space that is allocated to the variable cout << "Address of x in memory: " << &x; return 0; }
• We will get a result similar to the following when running.
Address of x in memory: 0x61fe1c
The following image shows how the address and value of the variable are stored x
in memory.
Bind two variables to the same address in memory inC++
If you define a variable and want to access it directly with a different name, you can define another variable and make it point to its address in memory as in the following example.
first example
#include <iostream> using namespace std; int main() { // and its value is x. Here we have defined a variable named int x = 5; // in memory x points to the same address as variable y here we have defined a variable named int &y = x; // x is the same as the value of the variable y and notice how the value of the variable .y and x here we have printed the value of the two variables cout << "x = " << x << endl; cout << "y = " << y; return 0; }
• We will get the following result when running.
x = 5 y = 5
The following image shows how the variable y
points to the same value as the variable x
in memory.
When you bind two variables to the same value in memory, you are able to change them from either of them.
In the following example, we define the variable x
and then we define a variable name y
and give it the same valuex
.
Then we changed the common value between the two variables x
and y
through the variabley
.
Finally, we display the value it contains x
and the value it contains y
to check whether it has changed for both of them or not.
second example
#include <iostream> using namespace std; int main() { // and its value is x. Here we have defined a variable named int x = 5; // in memory x points to the same address as variable y here we have defined a variable named int &y = x; // x which is the same as the value of the variable y here we changed the value of the variable y = 7; // to check if they contain the same new value which is 7 y and x here we print the value of the two variables cout << "x = " << x << endl; cout << "y = " << y; return 0; }
• We will get the following result when running.
x = 7 y = 7
Notes and practical examples on the use of references inC++
The following rules are very important when dealing with references and you must memorize them so as not to make mistakes because of them:
When we create a reference for something, it is as if we are adding another name(Alias)This is something that cannot be cancelled.
When defining a reference, the address of the object whose value it refers to must be directly specified in memory.
After a reference is defined, the object whose value is referenced cannot be changed.
Errors may appear when dealing with references in C-Plus Plus
Here we have put for you several examples of mistakes you might make when dealing with references so that you can learn how to avoid them.
first error
In the following example, we have defined as a x
reference without specifying the name of the variable that will be a reference to it.
int &x;
This code will cause the following error to appear, which means that no x
initial value was given.
The initial value here means the name of the variable to which it will be x
a reference.
second error
In the following example, we have defined two variables, x
and, y
and put an initial value in each of them.
Then we define a reference for the variable x
namedz
.
Then we tried to make the reference z
become a reference to the variabley
.
int x = 5; int y = 7; int &z = x; // There is no problem here &z = y; // points to the value of a variable other than the one for which it was originally defined for z here it will cause a problem because we tried to make the reference
Since this code will cause the following error to appear when running the code because the variable the reference is pointing to cannot be changed z
after it has been defined.
Passing the object in memory directly to a function in C++
The following example teaches you how to pass an object in memory directly to a function.
Here we define a function namedswap
,When called, we pass two variables to it and it replaces their values.
Example
#include <iostream> using namespace std; // and it will fetch their values from memory b and a when called we pass it the address of two variables in place of the two parameters swap here we have defined a function named void swap(int &a, int &b) { // in memory b and a here the function will tweak the values of the two variables int temp = a; a = b; b = temp; } int main() { // its value is 5 y and its value is 3 and a variable named x here we have defined a variable named int x = 3; int y = 5; // it has to swap their values of y and x and pass the two variables swap() here we called the function swap(x, y); // whether their values have changed or not y and x here we have printed the value of the two variables cout << "x = " << x << endl; cout << "y = " << y; return 0; }
• We will get the following result when running.
x = 5 y = 3