struct Error {
byte code;
boolean active;
char msg[21];
};
struct ErrorStates {
Error ERR_A;
Error ERR_B;
// ... and so on
};
// I thought I could do this. But it wont compile :(
ErrorStates errors = {
.ERR_A = {.code=1, .msg="some error"},
.ERR_B = {.code=2, .msg="some other error"},
}
Is there a way to init the struct using the name of the fields (for the sake of code clairty)?
I don't know why you thought you could do that. The compiler knows what variables in the structure to store the data in. It doesn't need your help to figure that out.
Of course, this won't compile either, since you are not supplying all the initializers for the structure members, but you can work out what you want the missing values to be and supply them.
I know I could just list the values, but then I need to know in what order they shall appear - and that is exactly what I want to avoid. Supplying the name of the fields just makes the code easier to read and there's no risk to mix things up. My example here is very simple but the case is of course general.
Should I conclude there is no way to init a struct using its field names?
I know I could just list the values, but then I need to know in what order they shall appear - and that is exactly what I want to avoid.
There are no shortcuts. You do need to supply the data in the correct order, or write your own code to initialize the members.
Should I conclude there is no way to init a struct using its field names?
The field names must be used when you want to initialize one field at a time, in your code. They are not to be used when you want the compiler to do the initialization for you.
You can do that in C99, the newer version of C. For some silly reason, C++ is no longer a superset of C, and they didn't bother including designated initializers.
It really is a nice feature though.
When initializing arrays C++ will fill unprovided data with the last value you do provide. In this case you're using C character string syntax which automatically provides a trailing zero so the remainder of the array should be initialized with the trailing zero.