Reputation: 11
In this code, the "array" is an array of pointers to chars? Or something else?
struct tmep{
char (*array) [SIZE];
}
Thanks in advance :)
Upvotes: 1
Views: 115
Reputation: 183948
It's a pointer to an array of SIZE
char
s.
Declaration mimics use, so you evaluate the parenthesis first, (*array)
gives you a char[SIZE]
.
To allocate, the stable version is as usual
array = malloc(num_elements * sizeof *array);
to specify the size of each object (char[SIZE]
here) in the block by taking the sizeof
the dereferenced pointer. You don't need to change that allocation if the type changes e.g. to int (*)[SIZE]
.
If you want to specify the type,
array = malloc(num_elements * sizeof(char (*)[SIZE]));
This allocates - if malloc
succeeds - a block large enough for num_elements
arrays of SIZE
char
s, each of these arrays is accessed with
array[i]
and the char
s in the arrays in the block with
array[i][j]
Upvotes: 7