0

I know *ptr is a pointer variable called ptr

Does **ptr mean its a pointer to a pointer?

If that is true, what is the meaning of a pointer to a pointer?

Any pointers (pun intended) are very much appreciated.

Undefined Variable
  • 4,196
  • 10
  • 40
  • 69
  • Upvote for the pun ;) – joe_young Jun 19 '15 at 21:13
  • _"I know *ptr is a pointer variable called ptr"_ No it's not... – Lightness Races in Orbit Jun 19 '15 at 21:15
  • 1
    @LightnessRacesinOrbit: That's correct, if you read between the lines: "I know `*ptr` is (a declaration of) a pointer variable called `ptr`" – Ben Voigt Jun 19 '15 at 21:23
  • @BenVoigt: It's not a declaration either. You need the rest of the type. I concede that if you pretend that arbitrary missing words are present in the assertion then, yes, you may call it "correct"; on SO I find injecting the word "not" often helps in this regard ;) – Lightness Races in Orbit Jun 19 '15 at 21:29
  • @LightnessRacesinOrbit: in my question, would it be a pointer to a custom type called 'know'? :) just joking, please excuse the fact that I did not add a type. – Undefined Variable Jun 19 '15 at 21:34
  • @UndefinedVariable: It's not really a matter of "excusing" it; the point is that `*ptr` and, say, `char *ptr` are completely different things. So it matters. You should be as precise as possible, in general. – Lightness Races in Orbit Jun 19 '15 at 21:35
  • @LightnessRacesinOrbit: oh, actually I did not know they were different things. I guess since I come from PHP background which is very loosely typed, I do not realize that in other languages having the type mentioned is very important! Thank you for pointing that out. – Undefined Variable Jun 19 '15 at 21:41
  • @UndefinedVariable C has a "declaration follows use" model, which means that when you write `int **x`, you're saying that `**x` has type `int` (and therefore `x` is a pointer to pointer to `int`). That's why `*ptr` and `char *ptr` are different things. But then, maybe, just *maybe*, "declaration follows use" was not the great idea that it seemed at the time, but it just stuck around. You just have to get used. – Filipe Gonçalves Jun 19 '15 at 21:46
  • @UndefinedVariable: Exactly: be precise because you may not know what difference it makes :) – Lightness Races in Orbit Jun 19 '15 at 22:05
  • @FilipeGonçalves: That's a fact of both C and C++ grammars, but whether you follow it in your source code is purely subjective. It's also not quite why `char *ptr` and `*ptr` are different things. – Lightness Races in Orbit Jun 19 '15 at 22:06
  • @LightnessRacesinOrbit Sure - they are different because one is a declaration and the other is not. I was just trying to help him by showing how `**ptr` can have different meanings depending on the context. – Filipe Gonçalves Jun 19 '15 at 22:08

1 Answers1

3

It depends on the context.

In a declaration, X **ptr declares an object that is a pointer to a pointer to X.

Outside of a declaration, **ptr dereferences both pointers, yielding the value.

Remember that a pointer is just an object that holds the address of another object. So a pointer to pointer is just that: an object that holds the address of another object, which happens to be a pointer.

Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
  • Thank you for that explanation. I think I understand it (kind of...) The thing is, I saw this line: int main(int argv, char **args).. in a code and I was wonder what char **args actually meant, or why it was used... – Undefined Variable Jun 19 '15 at 21:32
  • @UndefinedVariable Yes, strictly speaking, it's declaring a pointer to pointer to `char`, but in the case of `main()`, `args` is an array of pointers to `char`. So, `char *argv[]`. This is equivalent to `char **args` because array names decay into a pointer to their first element in most cases, so `char *argv[]` and `char **argv` are equivalent here. Just think of it as `char *argv[]` and hopefully it will be clear. – Filipe Gonçalves Jun 19 '15 at 21:42