I need to transfer packets through the internet whose length should be dynamic.
struct packet
{
int id;
int filename_len;
char filename[];
};
The problem is that zero-length arrays are not ISO-compliant.
Should I use char filename[1]; instead? But then sizeof(struct packet) will not return the correct value anymore.
Classic issue. You can simply deal with it (and note that sizeof(foo) may be off by more than one if the compiler rounds the structure size up, which is (I believe) allowed), or you can do something like this:
This is annoying (you have to use h.id, etc), but it works. Usually I just deal with it being one, but the above might be marginally more portable.