What value does pointer to pointer output if no dereferencing operator is used?

I have tried the output of the following code. But I don't yet understand what the q value might represent.

I understand that * q points to p, implying that printing * q will print the address p, where when ** q will print the value at the address pp, to, i.e. 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 is just q? What is the value that is printed when only q is printed?

PS: I'm new to C ++ and have been looking for an answer to this question elsewhere and don't seem to find it. If you find the answer to this question elsewhere, please forgive me and let me know and I will delete the question.

+3


source to share


7 replies


You speak:

print * q will print address p



However, it is not. Print q

will print the address p

. After all, you assigned it on runtime q = &p;

.

Print *q

will print the value p

, which is the address x

.

+1


source


Just print the 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

      

+2


source


Since it q

is a pointer to p

, value q

is a location p

, whatever that means on that particular platform. (Most likely the memory address containing the p

value.)

+1


source


(I'll take 64-bit pointers and 32-bit integers in this answer to have specific values ​​to write).

Let's analyze first p

. p

- 8 bytes storing the address (variable x

). Print *p

will print the value at that address, which is 4 bytes of the variable's value x

. Print p

will only print 8 bytes of the address stored in p

.

Now the same applies to q

. q

- 8 bytes storing the address of the variable p

. Therefore, printing *p

will print the value at that address, which is 8 bytes of the value p

. Print q

will only print 8 bytes of the address stored in q

.

+1


source


int x = 1;

The memory block is for storing the int value. And this block is called "x". Let's say x stands out at 0x1234.

int * p;

Here 'p' is a pointer to an int, which means that p will contain the address of some int. Let's say that p is allocated with address 0x2345.

p = &x;

      

This will make p contain address x ie 0x1234 will be stored in the location allocated by p.

int **q;

      

Here q is a pointer to a pointer to int, which means that q will contain the address of a pointer to int. Let's say that q is allocated with address 0x3456.

q = &p;

      

This will cause q to contain the address p ie 0x2345 will be stored in the location allocated for q.

I hope I am simple and straightforward ...

+1


source


A pointer is just a data type that is not very different from other types. It stores the address of another data object and you can use * p to access the data object. Thus, pointers are no different from other types, but their content has some special meaning.

(int **) is a pointer that points to a data object of type (int *), not very different from other pointers (they can point to a data object of type int).

So the content of q is a pointer to p. * q indicates that you are using content to search for p. the content of p is a pointer to an Integer, * p indicates that you are using the content to find an integer.

+1


source


By adding ** before q to the q declaration, you are telling the compiler that q is a pointer that will point to the address of the pointer. So when you type q, you are actually printing the address of the p-pointer, not the value of the address p pointing to (e.g. x).

In your code, p is a pointer that stores the address of an integer. But p itself has an address that you store in q.

+1


source







All Articles