Reference and Memory Adress in C++|| C++ Complete Course || Beginners to Advance| Urdu/Hindi | BJM T

Published: 28 November 2022
on channel: BJM Technologies
21
4

A reference variable is a "reference" to an existing variable, and it is created with the & operator:

string food = "Pizza"; // food variable
string &meal = food; // reference to food
Now, we can use either the variable name food or the reference name meal to refer to the food variable:

Example
string food = "Pizza";
string &meal = food;
Memory Address
In the example from the previous page, the & operator was used to create a reference variable. But it can also be used to get the memory address of a variable; which is the location of where the variable is stored on the computer.

When a variable is created in C++, a memory address is assigned to the variable. And when we assign a value to the variable, it is stored in this memory address.

To access it, use the & operator, and the result will represent where the variable is stored:

Example
string food = "Pizza";

cout (( &food; // Outputs 0x6dfed4
And why is it useful to know the memory address?
References and Pointers (which you will learn about in the next chapter) are important in C++, because they give you the ability to manipulate the data in the computer's memory - which can reduce the code and improve the performance.

These two features are one of the things that make C++ stand out from other programming languages, like Python and Java.


Watch video Reference and Memory Adress in C++|| C++ Complete Course || Beginners to Advance| Urdu/Hindi | BJM T online without registration, duration hours minute second in high quality. This video was added by user BJM Technologies 28 November 2022, don't forget to share it with your friends and acquaintances, it has been viewed on our site 2 once and liked it people.