PDA

View Full Version : cofused on pointers



baray98
9th November 2008, 07:12
guys,

what does this definition mean?

MyGrid ***grid;

please enlighten me

baray98

lyuts
9th November 2008, 10:03
guys,

what does this definition mean?

MyGrid ***grid;

please enlighten me

baray98


It means that grid - is a pointer which points at another pointer that points at one more pointer on MyGrid object.

Brandybuck
9th November 2008, 18:59
I've never seen a triple pointer before, but there's no reason why not. A double pointer is somewhat common, it means a pointer to a pointer. Why would you want a pointer to a pointer? Consider c-style strings.

char *mystring
char **ptr2mystring;

Also, arrays are really pointers to the first element of the array. So a common idiom is to write the following:

int main(int argc, char* argv[])

which is an array of strings as...

int main(int argc, char** argv)

But a triple pointer is much rarer. I would avoid it for the confusion it would cause. I would also avoid double pointers in C++ code, though they might be necessary in C code.

caduel
9th November 2008, 19:21
It is not possible to say what the intention of a triple pointer is from the pointer itself - because C does not distinguish between pointers and arrays.

It might be something not such sensible as the adress of the address of the adress of some object.
Or it might be: a 3-dim array.
int x; // an integer
int *x; // address of an integer OR first element of an array (size unknown)
int **x; // address of adress of integer; or address of array of int; or array of arrays of int
int ***x; // ... or array of array of array of int

(Note that the following is not correct
int field[10][5][3];
int ***x = field;

an array of pointers is how one realizes in low-level, plain C a dynamic array of unknown size. Note that the rows may have different sizes: each is the result of a malloc with potentially different length.
These things are not difficult but error prone. Therefore I recommend to stay clear of this and use C++/Qt containers when possible.)