Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
named vs positional initialization
#1
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



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 */
Reply


Messages In This Thread
named vs positional initialization - by delirium5223 - 04-09-2025, 11:27 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)