Rule 11.8: non-compatible pointees

Moderators: misra-c, david ward

Post Reply
pmhill
Posts: 4
Joined: Wed Apr 08, 2009 9:17 am
Company: University of Leeds

Rule 11.8: non-compatible pointees

Post by pmhill » Thu Jan 05, 2017 4:58 pm

In the following code I would like to know which of the casts are compliant for rule 11.8?

Code: Select all

const int a[10];
int *pi;
int **ppi;
const int **cppi;
const float **cppf;

void f() {
  pi = (int*) &a; /* int * <- const int (*)[10] */
  ppi = (int**) cppi; /* int ** <- const int ** */
  ppi = (int**) cppf; /* int ** <- const float ** */
}
For all the casts, the source pointee and destination pointee are not compatible, but:
- in first case pointees have different const qualifications i.e. we are converting a pointer to a const object to a pointer to a non-const object.
- in second case pointees have the same const qualifications i.e. we are converting a pointer to non-const object to a pointer to non-const object (only inner qualification differs)
- in third case pointees have the same const qualifications i.e. we are converting a pointer to non-const object to a pointer to non-const object (but everything else differs).

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

Re: Rule 11.8: non-compatible pointees

Post by misra-c » Thu Mar 30, 2017 8:25 am

This rule only applies to the type qualifiers of the type pointed to by the top-level pointer.
For example:
It is a violation of this rule to cast an object of type "X const *" to "Y *".
The presence/absence of type qualifiers within X or Y are covered by rule 11.3 and not by this rule.

For example:

Code: Select all

"const int **ppci" is a "pointer to pointer to const int". 
         The "pointer to const int" has no type qualifiers.
"int *const *pcpi" is a "pointer to const pointer to int".  
        The "pointer to int" has a const type qualifier
Performing the cast "( const int**)(pcpi)"  would cast away the const and violate this rule.
Looking at the specific examples

Code: Select all

pi = (int*) &a;   
  Casts "ptr to array of const int" to "ptr to int".  
  Compliant: no top-level type qualifier removed
            
ppi = (int**) cppi; 
  Casts "ptr to ptr to const int" to "ptr to ptr to int"
  Compliant: no top-level type qualifier removed

ppi = (int**) cppf; 
  Casts "ptr to ptr to const float" to "ptr to ptr to int"
  Compliant: no top-level type qualifier removed
All these examples would violate rule 11.3
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “8.11 Pointer type conversions”