09-12-2009, 02:02 PM
According to Rule 10.1 an initialiser for an object of unsigned type should be a constant expression of unsigned type (e.g. 0U). This is straightforward when initializing a scalar object; for example:
In the case of an "agggregate type" ( i.e. a struct or array), Rule 9.2 says:
This recognises that fact that ISO:C 6.5.7 says that for an automatic object of aggregate type, "if there are fewer initializers in a brace-enclosed list than there are members of an aggregate, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration" (i.e. each element or member is assigned the value 0).
So, strictly speaking, the example provide is not MISRA compliant; the initializer should really be "0U".
It is worth mentioning that the use of the unsigned char type in a bit-field is not permitted according to Rule 6.4. The only types permitted by Rule 6.4 are unsigned int and signed int.
Code:
unsigned char ux = 0U;
In the case of an "agggregate type" ( i.e. a struct or array), Rule 9.2 says:
Quote:Note also that all the elements of arrays or structures can be initialised (to zero or NULL) by giving an explicit initialiser for the first element only. If this method of initialisation is chosen then the first element should be initialised to zero (or NULL) ...
This recognises that fact that ISO:C 6.5.7 says that for an automatic object of aggregate type, "if there are fewer initializers in a brace-enclosed list than there are members of an aggregate, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration" (i.e. each element or member is assigned the value 0).
So, strictly speaking, the example provide is not MISRA compliant; the initializer should really be "0U".
It is worth mentioning that the use of the unsigned char type in a bit-field is not permitted according to Rule 6.4. The only types permitted by Rule 6.4 are unsigned int and signed int.
Posted by and on behalf of the MISRA C Working Group