Skip to main content

IAR Embedded Workbench for RISC-V 3.40

MISRAC2012-Rule-9.6

In this section:
Synopsis

(Required) An initializer using chained designators shall not contain initializers without designators.

Enabled by default

Yes

Severity/Certainty

Medium/Medium

mediummedium.png
Full description

Chained designators initializer without designators.

Coding standards
MISRA C:2012 Rule-9.6

(Required) An initializer using chained designators shall not contain initializers without designators

Code examples

The following code example fails the check and will give a warning:

struct S {
  int x;
  int y;
};

struct T { 
  int w; 
  struct S s; 
  int z;
};

void example() {
   /* 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 */
}

The following code example passes the check and will not give a warning about this issue:

struct S {
  int x;
  int y;
};

struct T { 
  int w; 
  struct S s; 
  int z;
};

void example() {
   /* Compliant */
   struct T tt = { 
      .w = 1, 
      .s.x = 2,
      .s.y = 3,
      .z = 3,
   };
}