[previous] MISRA-C:2004  Rule  11.3:  (Advisory) [next] A cast should not be performed between a pointer type and an integral type.
The size of integer that is required when a pointer is converted to an integer is implementation-defined. Casting between a pointer and an integer type should be avoided where possible, but may be unavoidable when addressing memory mapped registers or other hardware specific features.

Example Code:


/* PRQA S 2961,2981,2982,2983,3112 ++ */

#include "misra.h"
#include "m2cmex.h"

extern S16 test_1103( void )
{
   U32           prtadd = 0xFFF12345U;
   S32          *pi;
   volatile S32 *pvi;
   void (*pf)(void);

   pi  = (S32 *)prtadd;                     /* MISRA Violation */
   pvi = (volatile S32 *)prtadd;            /* MISRA Violation */
   pf  = (void (*)(void))prtadd;            /* MISRA Violation */

   u32a = (U32)pi;                          /* MISRA Violation */
   u32a = (U32)pvi;                         /* MISRA Violation */
   u32a = (U32)pf;                          /* MISRA Violation */

   bla = pi;                                /* MISRA Violation */
   bla = &(pi[1]);                          /* MISRA Violation */

   (BL) pi;                                 /* MISRA Violation */
   (BL) (pi + 1);                           /* MISRA Violation */

   (S16 *) (s16a < 0);                      /* MISRA Violation */

   return 0;
}


QAC messages that encompass this guideline:

0303 [I] Cast between a pointer to volatile object and an integral type.
0305 [I] Cast between a pointer to function and an integral type.
0306 [I] Cast between a pointer to object and an integral type.
0360 An expression of pointer type is being converted to type _Bool on assignment.
0361 An expression of pointer type is being cast to type _Bool.
0362 An expression of essentially Boolean type is being cast to a pointer.


Related rules:

Rule  3.1 All usage of implementation-defined behaviour shall be documented.



(c) The Motor Industry Research Association, 2004
QA C Source Code Analyser 8.1.2
MISRA-C:2004 Compliance Module 3.2
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents