Every variable that is created has its own memory address.
A memory address is the location where a variable is stored on the computer.
When we assign a value to the variable, it is stored in this memory address.
To access the memory address of a variable, we can use the operator &.
In C++, a variable that stores the memory address of a variable is called a pointer.
Syntax:
std::cout << &variable_name;
Example:
#include <iostream> int main(){ int myNum = 16; std::cout << &myNum; return 0; }
Output:
0x7ffe874a510c
The memory address is represented in hexadecimal form and the variable's memory address may differ from a computer to another, so when you run this code on your computer, it's likely to have a different result from the one in the output example.
As explained in the memory address section, a variable that stores the memory
address of a variable is called a pointer. Defining a pointer variable is the
same as defining other variables, except you have to write * after the variable
type and before the variable name. The variable type of the pointer has to be
the same as the variable type of the variable that the pointer points to.
Syntax:
variable_type variable_name1; variable_type* pointer_name1 = &variable_name1;
You can also print the variable that the pointer points by dereferencing by using
* before the variable name in the cout statement.
If we don't use *, then the compiler will print the memory address of the variable.
Syntax:
variable_type variable_name1; variable_type* pointer_name1 = &variable_name1; //print the memory address of variable_name1 std::cout << pointer_name1 << std::endl; //print variable_name1 std::cout << *pointer_name1;
Example:
#include<iostream> #include<string> int main(){ //variable declaration std::string myString = "Car"; int myAge = 18; //pointer declaration std::string* myptr1 = &myString; int* myptr2 = &myAge; std::cout << myptr1 << std::endl; //print memory address of myString std::cout << *myptr1 << std::endl;; //dereferencing (print myString) std::cout << myptr2 << std::endl;; //print memory address of myAge std::cout << *myptr2 << std::endl;; //dereferencing (print myAge) *myptr2 = 20; std::cout << myptr2 << std::endl;; //memory address is still same std::cout << *myptr2 << std::endl;; //the value of myAge changed return 0; }
Output:
0x7fffcc479cd0
Car
0x7fffcc479ccc
18
0x7fffcc479ccc
20
In C++, the name of an array is actually a pointer to the first element of the array.
In other words, when we print array_name
is actually the same value as printing &array_name[0].
Therefore, we can access the elements in an array by dereferencing.
Example:
#include <iostream> int main(){ int myNumbers[4] = {25, 50, 75, 100}; // Change the value of the second element to 23 *(myNumbers + 1) = 23; std::cout << *myNumbers << " "; //print myNumbers[0] std::cout << *(myNumbers+1) << std::endl; //print myNumbers[1] std::cout << myNumbers << std::endl; std::cout << &myNumbers[0]; //notice that both are the same return 0; }
Output:
25 23
0x7fffc29fd180
0x7fffc29fd180
A reference variable is a "reference" to a variable.
It has the same memory address as the variable that is referenced.
Defining a reference variable uses the operator & and has to
have the same variable type as the variable that is being referenced.
Syntax:
variable_type variable1; //referencing variable_type &variable2 = variable1;
If we change the variable2 to a new value,
then variable1 's value will also change.
Example:
#include <iostream> #include <string> int main() { std::string food = "Pizza"; //variable declaration std::string &meal = food; //reference declaration std::cout << food << "\n"; std::cout << meal << "\n"; meal = "rice"; std::cout << food << "\n"; //food will also change to rice std::cout << meal; return 0; }
Output:
Pizza
Pizza
rice
rice