I need to transfer packets through the internet whose length should be dynamic.
struct packet
{
int id;
int filename_len;
char filename[];
};
>
I suggest to use char filename[1]
and include a terminating 0-byte. This way, you can malloc()
the correct structure size and avoid one-off errors like this:
ptr = malloc(sizeof(struct packet)+filename_len);
strncpy(&ptr->filename, filename, filename_len);
But the receiver must to know that it needs to read filename_len+1
bytes.