Rule 11.3 pointer to array of const element

Moderators: misra-c, david ward

Post Reply
grunwald
Posts: 15
Joined: Wed May 14, 2014 1:02 pm
Company: Axivion GmbH

Rule 11.3 pointer to array of const element

Post by grunwald » Thu Apr 06, 2017 2:55 pm

Code: Select all

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.

dg1980
Posts: 109
Joined: Wed Apr 27, 2016 2:33 pm
Company: Elektrobit Automotive GmbH

Re: Rule 11.3 pointer to array of const element

Post by dg1980 » Tue Apr 11, 2017 7:46 am

Adding const/volatile is always safe and permitted by 11.3 because it applies to unqualified types only:
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.

Post Reply

Return to “8.11 Pointer type conversions”