I want to initialize a struct element, split in declaration and initialization. This is what I have:
typedef struct MY_TYPE {
bool flag;
short int value;
double stuff;
} MY_TYPE;
void function(void) {
MY_TYPE a;
...
a = { true, 15, 0.123 }
}
Is this the way to declare and initialize a local variable of MY_TYPE
in accordance with C programming language standards (C89, C90, C99, C11, etc.)? Or is there anything better or at least working?
Update I ended up having a static initialization element where I set every subelement according to my needs.
I found another way to initialize structs.
The struct:
Initialization:
As per GCC’s documentation, this syntax is obsolete since GCC 2.5.
I see you've already received an answer about ANSI C 99, so I'll throw a bone about ANSI C 89. ANSI C 89 allows you to initialize a struct this way:
An important thing to remember, at the moment you initialize even one object/ variable in the struct, all of its other variables will be initialized to default value.
If you don't initialize the values in your struct, all variables will contain "garbage values".
Good luck!