MISRA C:2012 Rule 9.6
An initializer using chained designators shall not contain initializers without designators
Since R2025a
Description
Rule Definition
An initializer using chained designators shall not contain initializers without designators.1
Rationale
Using chained designators in initializers allows you to initialize nested structures and arrays explicitly. Mixing chained designators with positional initialization is confusing and unclear.
To avoid confusing and unclear code, avoid mixing chained designators with positional initializers. This rule applies to every level of a nested object. That is, mixing chained designators and positional initializers at subobject level is a violation of this rule.
Polyspace Implementation
Polyspace® reports a violation if the initialization of a structure or an array mixes chained designators and positional initializers.
If you omit designators in a braced subobject initializer, Polyspace reports a violation even if the braced initializer does not contain any
chained designators. In this code, Polyspace reports a violation because the object var
initializes its
objects using chained designators but the subobjects of var::b
are
initialized using positional
initialization:
struct SO1 {
int a;
int b;
};
struct SO2 {
int x;
struct SO1 y;
int z[2];
};
struct myStruct {
int a;
struct SO2 b;
int c[2];
};
struct myStruct var = { //Noncompliant
.a = 1,
.b.x = 2,
.b.y = {3, 4},
.b.z = {5, 6},
.c = {7, 8}
};
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Initialization |
Category: Required |
AGC Category: Required |
Version History
Introduced in R2025a
1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.
The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:
MISRA C:2004
MISRA C:2012
MISRA C:2023
MISRA C++:2008
MISRA C++:2023
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.