I am trying to get rid of Rule 11.3 from my code.
Sample code:
static int32_t
do_test(const char *cp)
{
const char *c = cp;
const int32_t *x;
x = (const int32_t *)cp;
return *x;
}
I want the value of *c and *x to be same. Even-though the code is compiling and giving the correct value, "x = (int32_t *)cp;" causing violation of 11.3 and 11.8
Rule 11.3 violation: An object with pointer type shall not be converted to a pointer to a different object type.
I have tried with void pointer, but the result was not same as what I expected and also it resulted in additional violation.
Is there anyway to remove these violations ?
From MISRA C 2012 Document they are mentioning like there is an exception for this rule as it is permitted to convert a pointer to object type into a pointer to one of the object types char, signed char or unsigned char. The Standard guarantees that pointers to these types can be used to access the individual bytes of an object.
Ignore Dir 4.6 due to char type.
const int32_t *
. – Oliver Charlesworthstatic int32_t do_test(const void *cp) { const int32_t *x = cp; return *x; }
? – P.P*c
and*x
to be same" what do you want to happen ifc
points to something like:"\x01\x02\x03\x04"
? – Michael Burr