Is below code is compliant to rule 10.1? - Printable Version +- MISRA Discussion Forums (https://forum.misra.org.uk) +-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4) +--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17) +---- Forum: 6.10 Arithmetic Type Conversions (https://forum.misra.org.uk/forumdisplay.php?fid=37) +---- Thread: Is below code is compliant to rule 10.1? (/showthread.php?tid=686) |
Is below code is compliant to rule 10.1? - Pundlik Oulkar - 11-11-2009 //Example 1: struct xyz { unsigned char bit:1; }abc={0} ; // Is this assignment to bit-field is compliant to MISRA rule10.1 and Why? Re: Is below code is compliant to rule 10.1? - misra-c - 09-12-2009 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. |