[previous] 2771 [next] Definite: These pointers address different objects.
Pointers

A definite condition has been detected. An operation is being performed to compare or subtract the value of two pointers which address different objects.

Behaviour is undefined if an attempt is made to subtract pointers which do not address elements of the same array or to compare (with a relational operator) pointers which do not point to the same struct, union or array.

For example:


/*PRQA S 488,490,2017,2983,2991,2995,3132,3203,3355,3358,3408,3447 ++*/

extern int buf1[10];
extern int buf2[10];

extern void f2771(void)
{
    int * p1;
    int * p2;
    long  d;
    
    p1 = buf1;
    p2 = buf2;
    d = p2 - p1;      /* 2771 */
    
    if (p2 > p1)      /* 2771 */
    {
    }
}


MISRA C:2012 Rules applicable to message 2771:

Rule-18.2  (Required) Subtraction between pointers shall only be applied to pointers that address elements of the same array
Rule-18.3  (Required) The relational operators >, >=, < and <= shall not be applied to objects of pointer type except where they point into the same object


See also:

QA·C Source Code Analyser 8.1
MISRA C:2012 Compliance Module 1.0
© 2012 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index Contents