-4

This may be a duplicate of some other question/answer (which I couldn't find), but it seems to me that there are good explanations of why and when to use multi-pointers, but a simple explanation of how a pointer to a pointer is dereferenced is missing.

I have tried to make a simple (and vague) explanation, which should only give the reader the most basic knowledge of how to dereference pointers to pointers to pointers to ......

TomBombadil
  • 83
  • 1
  • 7

2 Answers2

3

Such an explanation shouldn't be needed. As you know that * dereferences the pointer, it's a simple induction that if the variable pointed to is a pointer, too, you can just dereference twice to get to this value. And so on.

Don't forget a simple rule of thumb: If you ever need more than double indirection (e.g. find yourself writing *** or worse), most likely your design is flawed and you should take one step back, redesign the solution to your problem and then implement again.

  • The intention was to give an easy understandable explanation of reading code containing multi-level pointers. Not necessarily something you've designed yourself - (since you should know what a multi-level pointer are before you can use it yourself). – TomBombadil Aug 07 '15 at 09:18
  • 1
    And I'd still argue that isn't needed. Given the information that `*` dereferences a pointer (along with what *dereference* means) and that pointers may point to other pointers, anyone not capable to deduce he can just write `**` doesn't qualify as a programmer. But that's just my opinion... –  Aug 07 '15 at 09:20
  • 1
    It is obviously opinion against opinion but my experience is that some new programmers (which I know uses this site a lot) have a hard time grasping the concept of pointers and it is not that rare that you stumble across multi-level pointers when you are learning programming. – TomBombadil Aug 07 '15 at 09:27
  • 3
    @MaltePedersen Playing with multiple levels of indirection is perfectly fine, and should in fact be done, when learning. Felix is talking about production code. – this Aug 07 '15 at 09:29
0

Say you have the following code:

int *p;
int **p2;
int k;

We can then initialize k and make p point to it. Afterwards we can let p2 point to the pointer p:

k = 10;
p = &i;
p2 = &p;

We will now dereference p2 twice to see what the value the pointer p2 points to are pointing at (using simple C++):

#include <iostream>
...
std::cout << "What is k? Answer: " << **p2;

--> What is k? Answer: 10

It is also possible to change the value of k by dereferencing it

**p2 = 19;
std::cout << "What is k now? Answer: " << **p2;

--> What is k now? Answer: 19

I hope this can help new programmers get an idea of how pointers to pointers can dereferenced. If triple (or more) pointers are wanted, simply dereference more levels

int ***p3 = &p2;
***p3 = 60;
std::cout << "What is k now? Answer: " << ***p3;

--> What is k now? Answer: 60

More information about what dereferencing really means can be found in

What does "dereferencing" a pointer mean?

Community
  • 1
  • 1
TomBombadil
  • 83
  • 1
  • 7