C++ pointer address explanation - c++

I am new to C++ and I have a piece of code like this:
int firstvalue=10;
int * mypointer;
mypointer = &firstvalue;
cout << "pointer is " << *mypointer << '\n';
cout << "pointer is " << mypointer << '\n';
cout << "pointer is " << &mypointer << '\n';
The result is:
pointer is 10
pointer is 0x7ffff8073cb4
pointer is 0x7ffff8073cb8
Can anyone explain to me why the result of "mypointer" and "&mypointer" are different?
Thanks a lot.

mypointer is the value of the variable mypointer. And that value is, due to your assignment, the address of firstvalue.
&mypointer is the address of the variable mypointer. That is, the address of mypointer.
So, mypointer is the address of firstvalue, and &mypointer is the address of mypointer. Since firstvalue and mypointer are distinct variables, they have different addresses.

See inscribed comments
int firstvalue=10; // first variable, stored at say location 2000, so &firstvalue is 2000
int * mypointer; // second variable, stored at say location 2004, so &mypointer is 2004
mypointer = &firstvalue; // mypointer had garbage, now has 2000
cout << "pointer is " << *mypointer << '\n'; // contents of mypointer i.e. firstvalue (10)
cout << "pointer is " << mypointer << '\n'; // value of mypointer i.e. 2000
cout << "pointer is " << &mypointer << '\n'; // address of mypointer i.e. 2004
got it?

In the example, the & operator means "the address of". Therefore "mypointer" is the address of the value 10, but "&mypointer" is an address of an address whose value is 10.

firstvalue is a variable which can hold an int type value. This variable has its own address 0x7ffff8073cb4.
myvariable is a (pointer) variable that can hold the an int * type value, i.e address of a variable that can hold int type value. This variable has its own address 0x7ffff8073cb8.

Related

Why do my pointers print different addresses? [duplicate]

#include <iostream>
using namespace std;
int main() {
int* z = new int(9);
cout << "address: " << z << endl;
cout << "value: " << *z << endl;
cout << "referance: " << &z << endl;
return 0;
}
Looking at the cout values, I was expecting the address and reference to give the same address, but heres what the output is:
address: 0x7fc452c032a0
value: 9
referance: 0x7fff5191b8d8
Just curious about the reason for this, is the plain value(z) the address of the variable in the heap with a value of 9, where var(&z) is address of the pointer variable which is located in the stack?
Here is a visualization:
Is the
&z designates the adress of the pointer int * z where you store the allocated adress new int(9).
The pointer z and the value 9 are stored at two different locations in memory.
There is not any notion of reference here, only adresses.
Let me go through some of the basics first.
A variable is a name that is used to refer to some location in the memory, a location that holds a value with which we are working.
Using '&' in C/C++ we can get the address of the variable.
A pointer is a variable that stores the address of a variable. For instance, in the example you are referring to
int* z = new int(9);
variable z stores the address of the value 9 [new int(9)].
Now, finally this variable has to be stored at some location in the memory and this can be accessed using ampersand (&).
&z //gives the address of the pointer to value 9 (address of variable z).
This is the same way the pointers and pointers to a pointer (multi level pointers) works.
int* z;
Above statement implies a pointer variable of int type declaration.
int* z = new int();
Above statement implies an address is allocated to pointer variable of int type dynamically.
int* z = new int(9);
Above statement implies value 9 is stored in a dynamically allocated.
cout << "address: " << z << endl;
Above line of code tells the address of pointer variable z.
cout << "value: " << *z << endl;
Above line of code tells the value stored in the variable z.
cout << "referance: " << &z << endl;
Above line of code tells the dynamically created variable's address.

Pointers and references

I am currently learning c++ and pointers inside of c++. I am curious as to why I get a different memory address when I reference a pointer and why it isn't the same as the reference to the variable. Here is my super simple code:
#include <iostream>
using namespace std;
int main() {
int n = 50;
int *p = &n;
cout << "Number: " << n << endl;
cout << "Number with &: " << &n << endl;
cout << "Pointer: " << p << endl;
cout << "Pointer with *: " << *p << endl;
cout << "Pointer with &: " << &p << endl;
}
Why does &p give me a different address than "&n" and "p"? Thank you
First you declare an int variable n with the value of 50. This value will be stored at an address in memory. When you print n you get the value that the variable n holds.
Then you declare a pointer *p pointing to the variable n. This is a new variable that will hold the address to the variable n, where it is stored in memory. When you print p you will get back that address.
You can also ask for the address to where the value of n is stored by using the & symbol. This is what you get when you print &n, this will be the same as the pointer p.
Using the * symbol before a pointer will dereference it, meaning instead of returning the address that is the pointer, it will return the value that the pointer is pointing to. In your case this is an int with the value 50.
Lastly, the pointer *p that you declared is stored at it's own address in memory. Just as you could get the address to the variable n with the & symbol, you can get the address of p by using &p. This will be different from the address of n since it's not where 50 is stored, it's where the pointer to the value 50 is stored.

Pointer Syntax Within Condition

After a pointer is initialized, do you have to use the * dereference operator to call the pointer in a condition?
Example:
int main()
{
int var = 10;
int *ptr = &var;
if(ptr) // does this need to be if(*ptr) ???
{.......}
}
And can I have a short explanation as to why?
Thank you.
if (ptr)
check if the pointer is not Null but
if (*ptr)
check if the value it points to is not zero (in this example is 10)
So for checking the value you shoud add *.
It depends on what you want to do.
if(ptr) checks if the pointer value is nullptr or not. Note that this is shorthand for if(ptr != nullptr).
if(*ptr) checks if what the pointer points to is nullptr (or 0) - and in that case, since you dereference (follow) the pointer to answer the question, the pointer itself had better not be nullptr in that case.
First of all, a pointer is only a variable. However, there are different contexts in which you can use it.
As any other variable you can access the pointers content (which is the adress of the underlying memory) as follows:
int i = 1;
int * p = &i;
std::cout << p << std::endl
this would output the adress of i since this is what is stored in p
If you however want to access the content of the underlying memory (the value of i), you need to dereference the pointer first by using the * operator:
std::cout << *p << std::endl;
This would print the value of iso 1.
of course you can also access the pointer's adress (since the adress of i is a numeric value as well and needs to be stored somewhere too):
std::cout << &p << std::endl;
That would output the adress of p so the adress where the adress of i is stored :)
As a little example try to run this code:
#include <iostream>
int main() {
int i = 1;
int * p = &i;
std::cout << "Value of i(i): " << i << std::endl
<< "Adress of i(&i): " << &i << std::endl
<< "Value of p(p): " << p << std::endl
<< "Dereferenced p(*p): " << *p << std::endl
<< "Adress of p(&p): " << &p << std::endl
<< "Dereferenced adress of p(*(&p)): " << *(&p) << std::endl;
}

Pointer variable, differences in var and &var

#include <iostream>
using namespace std;
int main() {
int* z = new int(9);
cout << "address: " << z << endl;
cout << "value: " << *z << endl;
cout << "referance: " << &z << endl;
return 0;
}
Looking at the cout values, I was expecting the address and reference to give the same address, but heres what the output is:
address: 0x7fc452c032a0
value: 9
referance: 0x7fff5191b8d8
Just curious about the reason for this, is the plain value(z) the address of the variable in the heap with a value of 9, where var(&z) is address of the pointer variable which is located in the stack?
Here is a visualization:
Is the
&z designates the adress of the pointer int * z where you store the allocated adress new int(9).
The pointer z and the value 9 are stored at two different locations in memory.
There is not any notion of reference here, only adresses.
Let me go through some of the basics first.
A variable is a name that is used to refer to some location in the memory, a location that holds a value with which we are working.
Using '&' in C/C++ we can get the address of the variable.
A pointer is a variable that stores the address of a variable. For instance, in the example you are referring to
int* z = new int(9);
variable z stores the address of the value 9 [new int(9)].
Now, finally this variable has to be stored at some location in the memory and this can be accessed using ampersand (&).
&z //gives the address of the pointer to value 9 (address of variable z).
This is the same way the pointers and pointers to a pointer (multi level pointers) works.
int* z;
Above statement implies a pointer variable of int type declaration.
int* z = new int();
Above statement implies an address is allocated to pointer variable of int type dynamically.
int* z = new int(9);
Above statement implies value 9 is stored in a dynamically allocated.
cout << "address: " << z << endl;
Above line of code tells the address of pointer variable z.
cout << "value: " << *z << endl;
Above line of code tells the value stored in the variable z.
cout << "referance: " << &z << endl;
Above line of code tells the dynamically created variable's address.

What is the address that appear when print a pointer without * or &

When print a pointer without * or & shows an address, I don't know what is this address.
For example:
int *n;
int num = 10;
n = &num;
cout << n << endl; // Prints 0020F81C
cout << &n << endl; // Prints 0020f828
The result:
I know cout << &n << endl; to print the address of place in the memory.
But what about cout << n << endl; ?
n is referring to "&num", therefore it's the address to the place in memory where num is pointing to.
You've answered your own question. It is an address. The address in memory of whatever the pointer points to, or zero: in this case, the address of 'num'.
Your second 'cout' prints the address of the pointer itself.