Is it legal to define array with 0 elements in structure or it is allowed as side effect by compilers? For example:
typedef struct _my_str
{
int len;
double total;
double tax;
int items;
double item_price[0];
} my_struct;
Is it legal to define array with 0 elements in structure or it is allowed as side effect by compilers? For example:
typedef struct _my_str
{
int len;
double total;
double tax;
int items;
double item_price[0];
} my_struct;
GCC allows zero length arrays as a last member of an struct.
ISO C99 changed this with flexible array member
typedef struct _my_str
{
int len;
double total;
double tax;
int items;
double item_price[]; // Flexible array member
} my_struct;
Note that this is not valid in C++ though.
In C you can use a variable array member but only as the last element of a struct: so in your case you can use double item_price[]; as the final struct element. But note that such a struct cannot be a member of another struct; not even as the final member.
In C++ it is not legal in any circumstance. But alternatives such as std::vector and std::array are adequate.