Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 11.3 pointer to array of const element
#1
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.
<t></t>
#2
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.
<t></t>


Forum Jump:


Users browsing this thread: 3 Guest(s)