MISRA Discussion Forums

Full Version: Is below code is compliant to rule 10.1?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
//Example 1:

struct xyz {
unsigned char bit:1;
}abc={0} ; // Is this assignment to bit-field is compliant to MISRA rule10.1 and Why?
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:
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.