[previous] MISRA C:2012  Rule-9.3:  (Required) [next] Arrays shall not be partially initialized

Amplification:

If any element of an array object or subobject is explicitly initialized, then the entire object or subobject shall be explicitly initialized.

Exception:

  1. An initializer of the form { 0 } may be used to explicitly initialise all elements of an array object or subobject.
  2. An array whose initializer consists only of designated initializers may be used, for example to perform a sparse initialization.
  3. An array initialized using a string literal does not need an initializer for every element.

Example Code:


#pragma PRQA_MESSAGES_OFF 1053,1298

#include "misra.h"
#include "m3cmex.h"

#define PI 3.141593f

static int32_t   a0903[4]    = { 1, 2, 3, 4};                  /*      */
static int32_t   b0903[4]    = { 1, 2, 3 };                    /* 0686 */


static int32_t   c0903[3]    = { 0, 1, 2 };                    /*      */
static int32_t   d0903[3]    = { 0, 1 };                       /* 0686 */
static float32_t e0903[4]    = { [1] = 1.0f, 2.0f };           /*      */      /* Non-compliant - t[0] and t[3] are implicitly initialized */
static float32_t f0903[50]   = { [1] = 1.0f, [25] = 2.0f };    /*      */      /* Compliant - designated initializers for sparse matrix */
static float32_t g0903[3][2] = {
                         { 0.0f, 0.0f },
                         { PI / 4.0f, - PI / 4.0f },
                         { 0 },                                /*      */      /* initializes all elements of array subobject arr[2] */
                               };
static char      h0903[10]   = "Hello";                        /*      */      /* Compliant by Exception 3 */

extern int16_t rule_0903( void )
{
   return 0;
}


QAC messages that encompass this guideline:

0686 Array has fewer initializers than its declared size. Default initialization is applied to the remainder of the array elements.



(c) The Motor Industry Research Association, 2012
QA C Source Code Analyser 8.1.2
MISRA C:2012 Compliance Module 1.0
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index Contents