View Full Version : Resolved about pointers

08-03-2009, 09:15 AM
I know
char a; =Variable
char* a; =Pointer
but what is
char** a;

08-03-2009, 09:46 AM
Simple! Pointer to a pointer type variable.

char a, *b, **c;

printf("%c %c %c", a, *b, **c);

08-03-2009, 09:51 AM
Simple! Pointer to a pointer type variable.

Why would one want to use that.. not used til now tats why... Like an array or pointers then i guess :)
thanks for clarification:thumbsup:

08-03-2009, 10:14 AM
well it can be quite useful, an easy example would be this function that changes the size of an array:

void changeArray(int** someArray, int arraySize)
delete[] (*someArray);
(*someArray) = new int[arraySize];

for (int i = 0; i < arraySize; i++)
(*someArray)[i] = i;

You can't rely on the fact that when you "re-create" the array, that it will be in the same place before (though it might if you don't create anything else on the free store after deleting and re-allocating it). I wouldn't recommend the code I posted though. You could use a C++ reference and you should use vectors for something like this instead because if the array is allocated at a different position than before, all other pointers pointing to that array are left pointing to a possibly deleted block of memory.

Basically if you want to change the adress that a pointer is pointing to using a function, you'll need a pointer to this pointer in order to change it, otherwise you would only be copying the adress its pointing to. But if you're programming in C++ you could just have a reference to a pointer as a parameter like so: int*&.