pointer to a pointer

I understand basic pointers but im having trouble with pointers that are written like this int **test. so its basically a pointer to a pointer? can someone give me an example how it might be useful to do such a thing or when it would be commonly used and possibly how i would get its value.
If you need to point at something, but it's already a pointer. For now, I'd just say understand what it's doing. Once you start getting into any API you'll start seeing them often enough
They are used in C (and when using C programming approaches in C++) in two common situations: one, to refer to dynamically-allocated arrays of pointers, as in
int** p = new int*[10];

and two, as an "out" parameter to a C function that modify pointers "by reference", in addition to returning some value

For example, the strtod(), strtol() and friends all take a char** parameter:

long strtol( const char *str, char **str_end, int base );

which you can call as

char str[] = "123 test";
char* end;
long n = strtol(str, &end, 10);

this function will change your end to point to the first non-converted character in the string held by str, in addition to returning the result of the conversion directly.

This kind of stuff is not useful in C++, where we have vectors and references.
Last edited on
It's very simple.
Sometimes, you may want to pass a pointer to a pointer. Pointers themselves are data and therefore take up space. The address of a pointer can be assigned to a pointer (to a pointer to data). Don't let the indirection fool you- it works the same way. You can dereference it and retrieve that pointer, and derefrence that too, to retrieve the data.
An array of pointers works in a similar way. With pointers, you usually want to read right-to-left.

int i; //i is an integer
int *ptr= &i; //address of i assigned to a pointer to an integer
int **pptr= &ptr; //address of ptr is assigned to a pointer to (a pointer to an integer).
**pptr=0; //i is zero
*pptr=0; //ptr is now NULL 
Topic archived. No new replies allowed.