Remix.run Logo
t-3 12 hours ago

No structs, just an array that accomplishes the same thing, without field names or other niceties. Enjoy the pleasure of not using a struct when you inevitably add/reduce/reorder fields later.

hibberl7 11 hours ago | parent [-]

Yeah this is nuts. It's like someone just discovered casting so they can be really clever and use the first pointer in an array for something non-pointy.

I can't make any sense of the aversion to a struct. Use a struct. You don't have to give its type a name, if that's the motivation here.

sparkie 10 hours ago | parent [-]

The reason the struct is avoided here is so the array can be typed to its element type (rather than casting to and from `void*`).

With a struct we would need one struct for each element type - at least prior to C23 which provides a better approach where we can declare the same struct multiple times in a translation unit.

    #define Array(T) struct array_##T { size_t len; T *elems; }
We can use `Array(int)` in multiple places in the same TU - but in C11 or earlier, this is an error.