I have tried the output of the following code. But, i don't yet understand what might the value of q represent.
I understand that the *q points to p implying that, printing *q would print the address of p, where as **q would print the value at the address p points to, ie, x(=5).
#include <iostream>
using namespace std;
int main()
{
int x=5;
int *p,**q;
p=&x;
q=&p;
cout<<q;
return 0;
}
So what does q alone represent? What is that value that is printed when just q is printed?
Just print value and ardess of your variable and you will see:
x: 5 &x: 0x7fff691dfcc4
p: 0x7fff691dfcc4 &p: 0x7fff691dfcb8
q: 0x7fff691dfcb8 &q: 0x7fff691dfcb0
&var - location;
var - value
Since q
is a pointer to p
, the value of q
is the location of p
, whatever that means on this particular platform. (Most likely the memory address containing p
's value.)
(I will assume 64-bit pointers and 32-bit integers in this answer, just to have concrete values to write about. It holds with any vaues, though).
Let's analyse p
first. p
is 8 bytes storing an address (of the variable x
). Printing *p
will print the value residing at that address, which is the 4 bytes of the value of variable x
. Printing just p
will print the 8 bytes of the address stored in p
.
Now, the same applies to q
. q
is 8 bytes storing an address of the variable p
. So printing *p
will print the value residing at that address, which is the 8 bytes of the value of p
. Printing just q
will print the 8 bytes of the address stored in q
.
int x = 1;
A block of memory is allocated to hold int value. And that block is named as 'x'. Let's say x is allocated at 0x1234.
int *p;
Here 'p' is pointer to int that means p would contain the address of some int. Let's say p is allocated address 0x2345.
p = &x;
This would make p contain the address of x ie 0x1234 would be stored in the location allotted to p.
int **q;
Here q is pointer to pointer to an int that means q would contain the address of pointer to int. Let's say q is allotted address 0x3456.
q = &p;
This would make q contain the address of p ie 0x2345 would be stored in the location allotted to q.
Hope I am simple and clear...
Pointer is just a data type, being not very different from other types. It stores an address of another data object, and you can use *p to access the data object. So, pointers are not different from other types but their contents have some special meaning.
(int**) is such a pointer that points to a data object of type (int*), being not very different from other pointers (they may point to data object of type int).
So, the content of q is a pointer to p. *q indicates you use the content to find p. the content of p is a pointer to an Integer, *p indicates you use the content to find the integer.
By adding ** before q in declaration of q, you are saying to compiler that, q is a pointer which will point to address of a pointer. So when you are printing q, you are actually printing address of p pointer, not the value of address p point to (ie x).
In your code, p is a pointer which is storing an address of an integer. But p itself has an address, which you are storing in q.
You say:
printing *q would print the address of p
However this is not true. Printing q
would print the address of p
. After all, you assigned that in doing q = &p;
.
Printing *q
would print the value of p
, which is the address of x
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.