0

I am quite confused on how this works:

int number = 10;
int* pointerOne = &number;
int* pointerTwo = pointerOne;

cout << *pointerTwo;

When I dereference pointerTwo, it should be printing pointerOnes memory address. Why is the compiler printing out the value of number?

Juansdk
  • 93
  • 5
  • `pointerTwo` is set be equal to `pointerOne`, that is, to point to the same object - namely `number`. – Igor Tandetnik Jan 24 '21 at 03:14
  • Correct, but when we defrence, we are accessing the value of what the pointer is point too. So in this instance, pointerTwo is pointing to pointerOne. – Juansdk Jan 24 '21 at 03:16
  • 1
    No. `pointerTwo` is pointing to what `pointerOne` was pointing to. In C++, `a=b` means set `a` to whatever `b` is. This works for pointers too. If `b` was pointing to something, `a` now points to the same thing. Which is the number, in your case. – Sam Varshavchik Jan 24 '21 at 03:17
  • 1
    Both pointers point to `number`, so when you dereference either of them, you get the value of `number`. `pointerTwo` is not pointing to `pointerOne` (if nothing else, it's the wrong type for that - just like you need `int*` to point to `int`, you need `int**` to point to `int*`) – Igor Tandetnik Jan 24 '21 at 03:17
  • Dereferencing `pointerTwo` does not change what it is. – Scott Hunter Jan 24 '21 at 03:18
  • A few links that provide basic discussions of pointers may help. [Difference between char *pp and (char*) p?](https://stackoverflow.com/a/60519053/3422102) and [Pointer to pointer of structs indexing out of bounds(?)...](https://stackoverflow.com/a/60639540/3422102) (ignore the titles, the answers discuss pointer basics) – David C. Rankin Jan 24 '21 at 05:56

3 Answers3

2

What you are trying to do could be achieved by:

int number = 10;
int* pointerOne = &number;
int** pointerTwo = &pointerOne;

cout << pointerTwo;

Pointers hold addresses. When you do pointerTwo = pointerOne; you are copying to pointerTwo the value pointerOne points. The address of a int*has type int**. You never touched pointerOne's address. You never used the & to extract it.

If you use pointerOne you will access the address. To access the place it points to you have to dereference: *pointerone.

So, when you dereference: *pointerTwo it should be equal to pointerOne, NOT to &pointerOne.

Check what the following commands prints:

cout << (*pointerTwo == pointerOne);
cout << '\n' << (pointerTwo == &pointerOne);
vmp
  • 2,370
  • 1
  • 13
  • 17
  • So I did this. and when I cout << &pointerOne and cout << *pointerTwo , I do not get the same values. – Juansdk Jan 24 '21 at 03:26
  • 1
    You are comparing different things... I will edit – vmp Jan 24 '21 at 03:32
  • `pointerOne's` type is `int*`.... `&pointerOne's` type is `int**`... `pointerTwo's` type is `int**`.... `*pointerTwo's` type is `ìnt*`... You are trying to compare a `int*` with a `int**` – vmp Jan 24 '21 at 03:43
  • I want the output to print pointerOne's memory address. II hope you understand now. @Juansdk – Juansdk Jan 24 '21 at 03:43
  • if I just print pointerOne, it will print the memory address of number. – Juansdk Jan 24 '21 at 03:45
  • 2
    That is what I told you... `pointerTwo` has the address of `pointerOne`... print `pointerTwo` without dereferencing. – vmp Jan 24 '21 at 03:48
  • Yes got it. Thank you so much! – Juansdk Jan 24 '21 at 03:49
  • You're welcome. If It solved your question, please mark the answer as the accepted one. – vmp Jan 24 '21 at 03:51
  • @DavidC.Rankin I didn't get your point. I didn't ask a question. – vmp Jan 24 '21 at 06:07
  • 1
    In your comment above mine, you ask the user to please accept the answer. You will need to do that often. The link I provided was for you to save for future use. Something like "After sufficient time, can you please take a look at [insert link here]". That is just a more structured way of asking the user to select the question, but also providing the proper link so the user will know how to do that. Many new users have no clue they need to click the box by your answer to select it. – David C. Rankin Jan 24 '21 at 06:19
0

because pointerOne and pointerTwo is same type, you just copying the value of pointerOne(the address of number) to pointerTwo

You must understand that the value it hold is not relates the address of it

Yud Bet
  • 1
  • 1
  • 2
0

You are assigning the address of number to pointerTwo as well through pointerOne.

sattva_venu
  • 677
  • 8
  • 22