[previous] MISRA C:2012  Rule-8.13:  (Advisory) [next] A pointer should point to a const-qualified type whenever possible

Amplification:

A pointer should point to a const-qualified type unless either:

For the purposes of simplicity, this rule is written in terms of pointers and the types that they point to. However, it applies equally to arrays and the types of the elements that they contain. An array should have elements with const-qualified type unless either:

Example Code:


#pragma PRQA_MESSAGES_OFF 2982,2983

#include "misra.h"
#include "m3cmex.h"

static int16_t rule_0813a( int16_t pba[],
                       int16_t pbb[],
                       int16_t pbc[],
                       int16_t pbd[],
                       int16_t pbe[],
                       const int16_t * const p );

static void rule_0813b( int16_t *x );
static int16_t  rule_0813c( const int16_t *x );
static void rule_0813d( void );


extern int16_t rule_0813( void )
{
   const int16_t ci = 99;
   int16_t ba[3] = { 1, 2, 3 };
   int16_t bb[3] = { 1, 2, 3 };
   int16_t bc[3] = { 1, 2, 3 };
   int16_t bd[3] = { 1, 2, 3 };
   int16_t be[3] = { 1, 2, 3 };

   int16_t r;

   r = rule_0813a( ba, bb, bc, bd, be, &ci );

   return r;
}

static int16_t rule_0813a(
   int16_t pba[],
   int16_t pbb[],                              /* 3673 */
   int16_t pbc[],                              /* 3673 */
   int16_t pbd[],
   int16_t pbe[],                              /* 3673 */
   const int16_t * const p )
{
   int16_t        r;
   int16_t       *pnc;
   const int16_t *pc;

   r = pba[ 1 ] + pbb[ 1 ] + pbc[ 1 ];

   rule_0813b( pba );                          /*      */       /* Prototype indicates pba is an output parameter */
   r = rule_0813c( pbb );

   pnc = pbd;                                  /*      */       /* pbd is addressed by a pointer to non-const */
   *pnc = 1;

   pc = pbe;                                   /*      */       /* pbe is addressed by a pointer to const */

   rule_0813d();

   r = *p;

   return r;
}

static void rule_0813d( void )
{
   int16_t buf[300] = {0};

   int16_t * pa = buf;
   int16_t * pb = buf;                         /*      */
   int16_t * pc = buf;                         /*      */
   int16_t * pd = buf;
   int16_t * pe = buf;                         /*      */
   const int16_t * const p = buf;
   int16_t r;
   int16_t *pnc;
   const int16_t *pci;

   r = *pa + *pb + *pc;

   rule_0813b( pa );                           /*      */       /* Prototype indicates pa is an output parameter */
   r = rule_0813c( pb );

   pnc = pd;                                   /*      */       /* pd is addressed by a pointer to non-const */
   *pnc = 1;

   pci = pe;                                   /*      */       /* pe is addressed by a pointer to const */

   r = *p;

   return;
}

static void rule_0813b( int16_t *x )
{
   *x = 0;
   return;
}

static int16_t rule_0813c( const int16_t *x )
{
   return *x;
}


QAC messages that encompass this guideline:

3673 The object addressed by the pointer parameter '%s' is not modified and so the pointer could be of type 'pointer to const'.



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