So say I want to initialise a new vec3
structure for a shape I want to render with random coordinates; in modern C the code would look something like struct vec3 coordinates = { rand_pos(), rand_pos(), rand_pos() };
. That's not the case with C90 however, which requires compile-time constants to initialise the struct
.
The only solution I could think of is to malloc
the struct
on the heap, initialise its members, and return its dereferenced value return *coordinates;
However, since I'm not returning a pointer - and I shouldn't return a pointer - it makes the API seem vague as I still need to free
the struct
later, but I'd be passing an address rather than a pointer to free
, which doesn't conform to the API Guidelines.
CodePudding user response:
There's nothing preventing you from defining the struct
without initialisation (or with compile time constants) and assigning the desired values afterwards. Initialization and assignment isn't the same, so any compiler will accept that
struct vec3 coordinates;
coordinates.x = rand_pos();
coordinates.y = rand_pos();
coordinates.z = rand_pos();
return coordinates;