int *p;
int **pp;
int a = 9;
p = &a;
pp = &p;
cout << "&a: " << &a
cout << "&p: " << &p
cout << "&pp: " << &pp
cout << "pp : " << pp
cout << "*pp: " << *pp
cout << "&*pp: " << &*pp
&&p and &&pp aren't defined in c++ so they are wrong using, but what &*pp is meaning? Is &*pp equalent to &&a?
When the program is starting, the result is as follows:
&a: 00AEFAE4
&p: 00AEFAFC
&pp: 00AEFAF0
pp : 00AEFAFC
*pp: 00AEFAE4
&*pp: 00AEFAFC (=&p ???)
On the other hand, why is &*pp equalent to &p?
you asked what does &*pp means here pp is a double pointer which contains address of the pointer (*p) by writing &*pp firstly you are dereferencing value of **pp
*(pp) which will become value inside pp which is the address of pointer p
by writing &(*pp) now you are trying to get address of the dereferenced value which is (address of p) that will ultimetely become address of p
in simple words where you use both of these operators together they cancel out each other and give you the value present in the pointer
in this case &* will be cancelled out and you will get value of pp which is address of p
i tried to make it as clear as i could.... hope that helps :)
Related
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.
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;
}
I'm trying to return the address of a pointer using the code below:
#include <iostream>
using namespace std;
int main(){
int x = 5;
int *p = &x;
//int **pp = &p;
cout << &p << endl;
return 0;
}
However what gets printed is 0x28fea8, which is the same address as x itself. Now if I uncomment the only commented line above and leave everything else as is, then what gets printed is 0x28fea4 which seems to be the correct address of the pointer.
My question is why do I need the uncommented line in order to display the correct address of my pointer?
Why do "cout << p << endl" and "cout << &p << endl" both display 0x28fea8 if that line is left commented?
Shouldn't "cout << &p << endl" display 0x28fea4 regardless of the other line?
I'm using Qt Creator as my compiler/ide if that helps. I downloaded my compiler from the following link: http://web.stanford.edu/~rawatson/qt/windows_install/index.html
EDIT: Ok how silly of me. My issue was that I was comparing addresses by changing values and then re-running the program. Instead I should have compared by printing them all in one program run. When I compare in one program I get different addresses for p and &p, as it should be.
I'm unable to reproduce what you are telling, with this code:
#include <cstdio>
using namespace std;
int main() {
int x = 5;
int* p = &x;
printf("x address: %p\n", &x);
printf("p value: %p\n", p);
printf("p address: %p\n", &p);
return 0;
}
I get the following output:
x address: 0xbfd6bba8
p value: 0xbfd6bba8
p address: 0xbfd6bbac
which is correct and indeed &p is &x+4 which is correct on a 32bit architecture since they are allocated on stack.
int x = 5;
int *p = &x; // b stores addres of variabile a
cout << "Adress of x is : " << p <<endl; // printing the adress of a
cout << "Value of x is : " << *p << endl; // printing the variabile stores in a
int **pp = &p; // pp stores thea adress of p
cout <<"Adress of p is" << pp << endl; //printing the adress of p
cout << "Adress of x is" <<*pp << endl; //printing the adress of x
return 0;
I hope that the comments solve your problem .
basic syntax of pointers: *ptr= &a
here &a will return the memory address of variable a and *ptr will store value of variable a
I want to ask, is it possible to make a pointer return a value from a given memory address? if yes what's the syntax
Yes, you can construct a pointer that refers to some arbitrary address in memory, by initialising the pointer with the address directly, instead of with an expression like &a:
int* ptr = (int*)0x1234ABCD; // hex for convenience
std::cout << *ptr;
Be careful, though, as it is rare that you know such a memory address exactly.
The cast (int*) is required as no implicit conversion exists between int and int*.
Yes. Use the dereference operator *.
For example;
int * a = new int;
int * b = new int;
*a = 5;
// Now a points to a memory location where the number 5 is stored.
b = a; //b now points to the same memory location.
cout << *b << endl; ///prints 5.
cout << a << " " << b << endl; //prints the same address.
int * c = new int;
c = *a;
// Now c points to another memory location than a, but the value is the same.
cout << *c << endl; ///prints 5.
cout << a << " " << c << endl; //prints different addresses.
I was just playing with pointers and found this weird thing. I created the pointer p and displayed its address as I did in the following program, the address of p and &p is different, why are they different?
int main()
{
int *p, s[5];
cout << p <<endl;
cout << &p <<endl;
p = s;
cout << p <<endl;
cout << &p <<endl;
}
P is a pointer that means it holds an address to an integer. So when you print p it displays address of the integer it points to. Whereas when you print &p you are actually printing the address of p itself rather than the address it points to.
As Jeeva said, pointer p also occupies a part of memory(the yellow part in this pic, its address is 0x300) and in this area, it holds the value which is the address of the array s. So p == 0x100 and &p == 0x300
If p were equal to &p, the pointer would point to itself, which is only possible with void pointers:
void* p = &p;
assert(p == &p);
I have yet to see a practical use for this, though :)
Oh wait, it also happens in self-referencing recursive data structures, which can be quite useful:
struct X
{
X* p;
void print()
{
std::cout << " p = " << p << '\n';
std::cout << "&p = " << &p << '\n';
}
};
X x;
x.p = &x;
x.print();
Think linked container, where the last node has itself as its successor. Output on my system:
p = 0x7fffb4455d40
&p = 0x7fffb4455d40
Why? Because an object usually starts at the same address in memory as its first data member.