I know the usage of zero length arrays and I want to know if the following is an acceptable use
struct foo {
int id_num;
bool is_person;
char person_name[0];
char product_name[0];
}foo;
and allocating as follows
#define NAME_SIZE 100
struct foo *data = (struct foo\*)malloc(sizeof(struct foo) + NAME_SIZE);
I have the above structure which is used to represent a Person. Now I want to use the same structure to represent a product. The structure will either refer a product or person based on the is_person flag. I don't want to put the last member inside a union as that will involve lot of code change in the existing code base. Therefore I modified it in the above way and it got compiled. I was wondering if this is a correct usage given the fact that I either use it as person or product and not both and I don't want to change the variable name to mean something generic like char person_or_product_name[0]
. I am also assuming that person_name
and product_name
acts as identifiers to the same region of memory.