Elias Zarka Nassar
Elias Zarka Nassar

Reputation: 11

What does this line in code mean? (array of pointers to chars)?

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

Answers (1)

Daniel Fischer
Daniel Fischer

Reputation: 183948

It's a pointer to an array of SIZE chars.

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 chars, each of these arrays is accessed with

array[i]

and the chars in the arrays in the block with

array[i][j]

Upvotes: 7

Related Questions