'Initializing array or Structure with zero causes misra violation to Rule11.9
I am using Klocworks to check MISRA violations on C99. When initializing an array of type u8(unsigned char) or a structure whose first element is of the same type, it shows a violation to MISRA Rule 11.9.
Example:
typedef unsigned char u8;
typedef {
u8 a;
u8 b;
u8 c;
} tmyStruct;
const tmyStruct myStruct = { // MISRA.LITERAL.NULL.PTR.CONST.2012(Rule 11.9)
.a = (u8)0,
.b = (u8)0,
.c = (u8)1
};
OR
typedef {
u16 a;
u8 b;
u8 c;
} tmyStruct;
tmySructArr[3] =
{
{
.a = (u16)0,
.b = (u8)0,
.c = (u8)1
},
{
.a = (u16)0,
.b = (u8)0,
.c = (u8)1
},
{
.a = (u16)0,
.b = (u8)0,
.c = (u8)1
},
};
Solution 1:[1]
Rule 11.9 of MISRA C:2012 states
The macro NULL shall be the only permitted form of integer null pointer constant.
While I see several zero value constants, I don't see any pointers, so I politely suggest that this is a false positive, and that you need to refer this to Klokwork/Perforce support.
PS: Please ditch the custom integer types, and use <stdint.h>
Please note profile for my affiliations!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrew |
