Let's say I have a struct made of several fields, all of the same basic data type.
For example:
struct myStruct {
float a;
float b;
float c;
float d;
float e;
float f;
}
Is there a smart approach to initialize or set all members to a given value, e.g. -1
, or 0xDEADBEEF
, in a way that is flexible to changes in the number of fields and in the field names?
Rationale:
Initializing all fields to an invalid state, and make sure all fields are initialized if later on I add new fields.
Note:
If there is a solution that would only work for integer types, I am anyway interested.
This is a different question from array initialization and zero-initialization of a struct, as here I am asking about initializing a struct, with fields all of the same basic data type, to a custom value.
This question, which doesn't concern arrays inside a struct, is also not answered at Initialize values of array in a struct. It's also not treated in structs in C with initial values, as I am asking about the case in which all data field have the same, basic, data type