4 ms·
That seemed pretty straightforward to me. A zero size array is better thought of as a pointer to that part of the struct, and if you put two back to back you g
by jandrese 11y ago
That seemed pretty straightforward to me. A zero size array is better thought of as a pointer to that part of the struct, and if you put two back to back you get two pointers to the same spot. I'm not sure what that guy was expecting to happen.
The usual reason to use them is when you have a protocol header with a variable length data portion. Putting the zero length array in the struct allows you easy access to the data while not changing the sizeof() the header so you can avoid a bit of pesky pointer math and make the code easier to read.
Thinking about this some more, you could also use this as a ghetto form of union, but I'm not sure why you would want to.
struct
{
char[0] theBytes;
int theValue;
}
- gohrt 11y agoThe guy was expecting a compiler error for bogus constructs, which C99 provides for flexible array member: https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
- ryao 11y agoA zero sized array can only be used at the end of the struct. It only makes sense there.
- michaelmior 11y agoYou can use zero sized arrays anywhere in a struct. However, I think you can make the argument that they don't really make sense anywhere.
- mzs 11y agoFunny, I used a union once to port code when I found-out zero sized arrays were a gcc thing: /* * SC C Compiler 8.8.4f1 (part of MPW 3.3) does not support zero sized * arrays. I use this 'clever' trick involving the preprocessor that * guarantees everything is aligned appropriately. It is possible with * this that some space will be wasted between the header and the * payload if the a single element of the payload is larger than the * header, but that is not the case here. */ #if 0 struct state { mbhdr_t st_mb; /* Screen 1, Line 1 */ short st_serln; /* Screen 1, Line 2 */ short st_astln; unsigned char st_count; unsigned char st_unit; char st_qs[2]; char st_units; /* Screen 1, Line 3 */ avunit_t st_avms[0]; }; #else struct state { union { struct { mbhdr_t suh_mb; /* Screen 1, Line 1 */ short suh_serln; /* Screen 1, Line 2 */ short suh_astln; unsigned char suh_count; unsigned char suh_unit; char suh_qs[2]; char suh_units; /* Screen 1, Line 3 */ } su_header; avunit_t su_align[1]; } st_union[1]; }; #define st_mb st_union[0].su_header.suh_mb #define st_serln st_union[0].su_header.suh_serln #define st_astln st_union[0].su_header.suh_astln #define st_count st_union[0].su_header.suh_count #define st_unit st_union[0].su_header.suh_unit #define st_qs st_union[0].su_header.suh_qs #define st_units st_union[0].su_header.suh_units #define st_avms st_union[1].su_align #endif
- _kst_ 11y ago> A zero size array is better thought of as a pointer to that part of the struct, ... An array of any size is best thought of as an array. Arrays are not pointers. (See section 6 of the comp.lang.c FAQ, http://www.c-faq.com/. http://www.c-faq.com/.) A zero size array is best thought of as illegal (if you want portability) or as a compiler-specific extension (if you don't mind depending on a particular compiler, perhaps gcc).
- jandrese 11y agoTechnically correct, but using the zero length array trick creates clearer, more self documenting code. It's a tradeoff between readability and portability.
- _kst_ 11y ago> Technically correct, Or, as I prefer to say, "correct". > but using the zero length array trick creates clearer, more self documenting code. It's a tradeoff between readability and portability. Clearer compared to which alternative? I find C99-style flexible array members (defined with "[]") quite clear.