MISRA Discussion Forums
Rule 11.3 pointer to array of const element - 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:2012 and MISRA C:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=21)
+---- Forum: 8.11 Pointer type conversions (https://forum.misra.org.uk/forumdisplay.php?fid=166)
+---- Thread: Rule 11.3 pointer to array of const element (/showthread.php?tid=1333)



Rule 11.3 pointer to array of const element - grunwald - 06-04-2017

Code:
static void use_array(uint8_t const (* const arr)[4])
{
}

int main()
{
    uint8_t aAction[4] = {0, 0, 0, 0};
    use_array((uint8_t const(*)[4]) &aAction); // 11.3 violation?
}

As I read rule 11.3, the cast in the use_array call violates rule 11.3: the target type of the pointer is "4-element array of const uint8_t". Given that the "const" is not at top-level, adding it is not permitted by this rule.

However, that raises the question: how can I call use_array() from Misra-compliant code?

Note that the cast is required by ISO C (gcc says: pointers to arrays with different qualifiers are incompatible in ISO C [-Wpedantic]).
However, in C++, an implicit conversion exists for this case.


Re: Rule 11.3 pointer to array of const element - dg1980 - 11-04-2017

Adding const/volatile is always safe and permitted by 11.3 because it applies to unqualified types only:

Quote:the rule applies to the unqualified types. It does not prevent type qualifiers from being added...

The non-compliant example at the bottom of 11.3 is very poorly chosen, because it has two levels of indirection, not one:
pointer to const pointer to int vs pointer to const pointer to const int

Your example is compliant IMHO, because it just adds const using one level of indirection.
I am curious about an official answer though.