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 .
Related
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 :)
int main()
{
int x;
x = int(&x);
int* q;
q = &x;
int* p;
p = (int*)&x;
int* w;
w = (int*)x;
cout << *p << endl << x << endl << *q << endl << *w << endl << p << endl << q << endl << w << endl << &x;
return 0;
}
OUTPUT:
5762632
5762632
5762632
5762632
0057EE48
0057EE48
0057EE48
0057EE48
Is this step x = int(&x) valid ? Is there anything conceptually wrong about this?
How to visualize/interpret what happens when we write p = (int*)&x and w = (int*)x?
Also note that this program executes only when step 1 is written. If x does not contain its own address, then the program will show an error.
Is this step x = int(&x) valid ?
Is there anything conceptually wrong about this?
The statement may be valid and could provoke undefined behavior. All depends on the platform.
On many of the platforms I've worked with, the addresses were never signed. They were always unsigned because the sign bit was needed to represent the full range of addresses. In other words, I suggest:
unsigned int x = (unsigned int) &x;
Again it's about the sign bit and how addresses are represented.
How to visualize/interpret what happens when we write p = (int*)&x and w = (int*)x?
If you want to print an address, I suggest you use the pointer format:
cout << ((void *) &x) << endl;
or
printf("%p\n", &x);
I recommend using a debugger rather than printing out addresses. The debugger allows you to examine the values in a pointer. But there are times when printing out information is a better method (like when working with multiple tasks or multiple processors).
I have the following code:
#include <iostream>
using namespace std;
int main ()
{
int myvar = 5;
int * p;
cout << "Hello2" << endl;
*p = myvar;
cout << "Hello" << endl;
cout << p << endl;
//cout << &myvar << endl;
}
I know I am not doing the right thing by not initializing the pointer. I was just playing with pointers and noticed this. The issue is when I comment out the last line, the program executes normally. But as soon as I uncomment the line, I get a segmentation fault. I don't know why printing address of myvar is causing this? Has myvar been modified in any way because of pointer dereferencing? I am using C++11.
int* p;
*p = myvar;
You are creating an uninitialized pointer and then derferencing that pointer. This has undefined behavior because p has to point to something for it to be derferenced correctly. Therefore your program's behavior can't be reasoned with.
Segmentation Fault occurs when trying to access a virtual memory address that has no read permissions.
In your case, the local variable p holds uninitialized garbage from the stack.
you are dereferencing a memory address that might not be readable(e.g no read permissions, hence the segmentation fault when trying to access it).
I'm not entirely sure the purpose of your snippet, but the following code will work, and perhaps it will help:
int myvar = 5;
int *p = nullptr;
p = &myvar;
cout << myvar << endl;
cout << &myvar << endl;
cout << p << endl;
cout << *p << endl;
(Note: I used two lines for setting 'p' because that is how you did it in your snippet. You could easily just use: int *p = &myvar; )
Anyway, there are scope issues here as p will only be valid as long as myvar is in scope; however, this does illustrate the basics of pointers. myvar and *p will return the same value (the value being pointed to), and &myvar and p will return the same value (the location of value in memory.)
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 = #
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.
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.