From 11410988b8b1cb7e3992d5f56726e30e8f3988f9 Mon Sep 17 00:00:00 2001 From: Dhairya Gothi <142989448+dhairyagothi@users.noreply.github.com> Date: Thu, 13 Jun 2024 15:09:19 +0530 Subject: [PATCH] c++ reference documentation added (#286) --- docs/day-14/Create-References.md | 32 ++++++++++++++++++++++++++++++++ docs/day-14/Memory-Address.md | 25 +++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 docs/day-14/Create-References.md create mode 100644 docs/day-14/Memory-Address.md diff --git a/docs/day-14/Create-References.md b/docs/day-14/Create-References.md new file mode 100644 index 000000000..7c2f3591f --- /dev/null +++ b/docs/day-14/Create-References.md @@ -0,0 +1,32 @@ +--- +sidebar_position: 1 +title: "C++ References" +description: "In this tutorial, we will learn about the hoe to create References in C++." +sidebar_label: "Create References" +slug: C++ References +--- + +## Creating References + +A reference variable is a "reference" to an existing variable, and it is created with the & operator: + + + ```cpp + 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 + + ```cpp + string food = "Pizza"; +string &meal = food; + +cout << food << "\n"; // Outputs Pizza +cout << meal << "\n"; // Outputs Pizza + + ``` + + \ No newline at end of file diff --git a/docs/day-14/Memory-Address.md b/docs/day-14/Memory-Address.md new file mode 100644 index 000000000..f4863e996 --- /dev/null +++ b/docs/day-14/Memory-Address.md @@ -0,0 +1,25 @@ +--- +sidebar_position: 2 +title: "C++ Memory Address" +description: "In this tutorial, we will learn about the concept of C++ memory Address ." +sidebar_label: "C++ Memory Address" +slug: memory address in c++ +--- + +## 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** + + ```cpp + string food = "Pizza"; + + cout << &food; // Outputs 0x6dfed4 + ``` + +Note: The memory address is in hexadecimal form (0x..). Note that you may not get the same result in your program.