+ 1
What is variable memory address used for in C++
I’ve just learned about memory address in my C++ tutorial and I want to know the uses
1 Resposta
+ 2
Imagine you want to pass a variable by reference. You can do it with the help of the variable's memory address e.g.:
Add(int* numb)
{
*numb += 100; //adds 100 to the variable
}
int main(){
int i = 0;
Add(&i);
cout << i << endl; //it's 100
return 0;
}
As you can see it adds 100 to i and since it's passed by reference type it affects the variable in the main function too.