The following structure is having a member
fields which is a array of size 0 of char*
#include <stdio.h>
struct _table_model_entry {
Please note that names starting with '_' are reserved for use by the
implementation at file scope. If this struct type is defined by the
implementation, you can use it, but you should not define it. If it is a
struct defined in user code, you should change the name to one which is
not reserved.
Some people get in the habit of using such names because they see such
names used in standard header files. Break that habit: only the
implementation can define such names at file scope.
You can use such names at block scope, but it's safer to avoid such
names completely.
struct _table_model_entry *next;
int line_nr;
int nr_fields;
char *fields[0]; /* User defined */
};
What is the use of such an array
This is one of three ways to do the same thing. This way works on
certain compilers, but only as a non-conforming extension.
The other way involves declaring an array of length 1, but using it as
if it were a longer array. This has undefined behavior, but if you
allocate sufficient memory to hold that longer array, on most (possibly
all) C90 compilers, it will work as intended.
The third way was added in C99. This involves leaving the array length
unspecified, creating what in C99 is called a "flexible array member".
Regardless of which approach you use, this can only be done on the very
last member of a struct. The only practical way to create a struct of
this type is in dynamically allocated memory. You could do so as follows:
struct table_model_entry tme = malloc((sizeof tme)
+ (number_of_fields-1) * sizeof tme->fields[0]);
Note that the '-1' is needed only if you use the fields[1], because in
that case (sizeof tme) includes enough space to store one field.
approach. It is unnecessary if you declare fields[0] or fields[].