What is ** in C++?
I've seen some code, as well as some errors generated from my compiler that have a '**
' token before the variable (eg **variablename unreferenced-- or something, I can't recall exactly offhand). I'm fairly certain this is related to pointers, if I had to guess it looks like it's trying to dereference twice. '**
' is fairly ungoogleable. Can someone point me to a good website/documentation or would someone care to explain it here?
Thanks.
Great responses. If I can add, what would be some situations where it is useful to have a pointer to a pointer? Shouldn't you just be using the original pointer instead of creating yet another pointer to the original pointer?
Solution 1:
**
is not actually only pointer to pointer (as in declaration), but is also the dereference of a dereference (in a statement).
It is used often in C which does not have the & notation for references, e.g. to update a return value which is a pointer type:
int alloc_foo(struct foo **foo_ret)
{
*foo_ret = malloc(sizeof(struct foo));
return 1; /* to indicate success; return value in foo_ret */
}
Solution 2:
You may recognize the signature for main():
int main(int argc, char* argv[])
The following is equivalent:
int main(int argc, char** argv)
In this case, argv is a pointer to an array of char*.
In C, the index operator [] is just another way of performing pointer arithmetic. For example,
foo[i]
produces the same code as
*(foo + i)
Solution 3:
It's not a **
token. It's simply a *
token followed by another *
token. In your case, you have a pointer to a pointer, and it's being dereferenced twice to get whatever's really being pointed to.