5 ms·
Why would you want to avoid using a struct? Add a macro that declares the appropriate struct and get at least a tiny bit of type checking. With some clever use
by procaryote 3mo ago
Why would you want to avoid using a struct? Add a macro that declares the appropriate struct and get at least a tiny bit of type checking.
With some clever use of _Generic you could even build specialised functions for that type and get pretty good type checking
- sparkie 3mo agoIn C23 this approach is nice, but in older versions of C we end up with awful macros where we need to define the structure before we use it. #define Array(T) struct array_##T #define DEFINE_ARRAY(T) struct array_##T { size_t len; T *elems; } DEFINE_ARRAY(int); Array(int) foo; Array(int) bar; C23 has relaxed rules for redefining the same struct, so we can avoid having to create the struct up front. #define Array(T) struct array_##T { size_t len; T *elems; } Array(int) foo; Array(int) bar;