Initialisation of arrays of floats

Moderators: misra-c, david ward

Post Reply
sdcandy
Posts: 1
Joined: Thu Jun 29, 2017 8:44 am
Company: SDC Systems Limited

Initialisation of arrays of floats

Post by sdcandy » Wed Jul 19, 2017 2:04 pm

Rule 9.3 in MISRA C:2012 has an exception which states "An initializer of the form { 0 } may be used to explicitly initialize all elements of an array object or subobject". This means that something of the form is acceptable:

Code: Select all

uint32_t x[3] = { 0 };
When using floating point types is it also acceptable to use something of the form:

Code: Select all

typedef double float64_t;
float64_t array_a[10] = { 0.0 };
-Andy.

misra-c
Posts: 556
Joined: Thu Jan 05, 2006 1:11 pm

Re: Initialisation of arrays of floats

Post by misra-c » Thu Sep 28, 2017 11:37 am

{ 0 } is a MISRA-C 2012 exception, which permits the initialization of the elements of any array to zero.
Initialisating an array of double with { 0.0 } is non-compliant, unless the array has only one element.

Code: Select all

float64_t array_a[10] = { 0.0 };                     // non-compliant
float64_t array_a[10] = { 0 };                       // compliant
float64_t array_a[10] = { 0.0, 0.0, 0.0, 0.0, 0.0, 
                          0.0, 0.0, 0.0, 0.0, 0.0};  // compliant
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “8.9 Initialization”