04-09-2025, 11:27 AM
(This post was last modified: 04-09-2025, 12:24 PM by delirium5223.)
What do you think about advising against using mixed aggregate initialization, named vs positional.
When mixing them there is a higher chance for human confusion/mistake. e.g. not covering all of them etc
Rule 9.6 only prohibits them when they are embedded structs, not for single struct.
When mixing them there is a higher chance for human confusion/mistake. e.g. not covering all of them etc
Code:
typedef struct
{
int x;
int y;
int z;
} TypeA;
void test4(void)
{
TypeA obj = { .y = 2, 3 }; // nok mixed aggregate initialization, named vs positional
TypeA obj2 = { 3 , .y = 2 }; // nok
TypeA obj3 = { 3 , 2, 4 }; //ok, not mixed
TypeA obj4 = { .x = 3 , .z = 4, .y = 2 }; //ok, not mixed
}
Rule 9.6 only prohibits them when they are embedded structs, not for single struct.
Code:
/* Non-compliant - chained designators and implicit positional initializers mixed */
struct T tt = {
1,
.s.x = 2, /* To a human reader, this looks like .z is being initialized */
3, /* tt is actually initialized as { 1, { 2, 3 }, 0 } */
}; /* This also violates Rule 9.2 */