Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Changed exception ARR02-EX0 to ARR02-EX1

...

The C Standard also dictates how array initialization is handled when the number of initialization elements does not equal the explicit array bound. Section 6.7.9, paragraphs 21–2221 and 22, states:

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

...

This noncompliant code example initializes an array of integers using an initialization with too many elements for the array.:

Code Block
bgColor#FFCCCC
langc
int a[3] = {1, 2, 3, 4};

...

This compliant solution explicitly specifies the array bound.:

Code Block
bgColor#ccccff
langc
int a[4] = {1, 2, 3, 4};

Explicitly specifying the array bound, although it is implicitly defined by an initializer, allows a compiler or other static analysis tool to issue a diagnostic if these values do not agree.

Exceptions

ARR02-EX0EX1: STR36-C. Do not specify the bound of a character array initialized with a string literal is a specific exception to this recommendation; it requires that the bound of a character array initialized with a string literal is unspecified.

...