Rule 11.3 - Unclear Example 3

Moderators: misra-c, david ward

Post Reply
Posts: 2
Joined: Fri Aug 09, 2013 12:26 pm
Company: ITK Engineering AG

Rule 11.3 - Unclear Example 3

Post by thohae » Mon Nov 10, 2014 3:27 pm

The third and last example of Rule 11.3 states that

Code: Select all

int * const * pcpi;
const int * const * pcpci;
pcpci = ( const int * const * ) pcpi;
is non-compliant because the unqualified pointer types are different, namely “pointer to const-qualified int” and “pointer to int”
But the rule's amplification explicitly states, that
This rule applies to the unqualified types that are pointed to by the pointers.
and that the rule's goal is to prevent misalignment.
I do not understand why the example is non-compliant, as the cast only adds a const-qualifier and leaves the base type as it is (i.e. "int").
Could you please explain this a little further?

Posts: 560
Joined: Thu Jan 05, 2006 1:11 pm

Re: Rule 11.3 - Unclear Example 3

Post by misra-c » Fri Dec 05, 2014 9:04 am

pcpi is a "pointer" to const qualified object of type "pointer to int"
pcpci is a "pointer" to const qualified object of type "pointer to const int"

The unqualified object type for pcpi is "pointer to int", which is different to the unqualified object type of pcpci which is "pointer to const int".

The phrase "unqualified types" only refers to the top-level const/volatile qualification. See for further discussions on unqualified pointer types.

The rule's rationale applies to more issues than misalignment as mentioned in the second paragraph of the rationale. Such an example could be

Code: Select all

const int ci = 10;
const int **ppci;         /* pointer to pointer to const int */
int *pi;
ppci = (const int **)π 
*ppci = &ci;             /* pi now points to const int */
*pi = 0;                 /* undefined - attempt to update a const int */
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “8.11 Pointer type conversions”