Main Content

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

expand all

In the function foo(), the structure circ uses chain designators for its member Center and then uses a positional initialization. The code can appear to defines circ.Center.y as 0 and circ.Radius as 4, when it actually defines circ.Center.y as 4 and circ.Radius as 0.

typedef struct {
	int x;
	int y;
} Point;

typedef struct {
	Point Center;
	double Radius;
} Circle;


void foo()
{
	Circle circ = { //Noncompliant
		.Center.x = 3,
		4,
	};
}

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.