[previous] 3440 [next] Using the value resulting from a ++ or -- operation.
Arithmetic type - Operations REFERENCE - ISO:C90-6.3.2.4 Postfix Increment and Decrement Operators, ISO:C90-6.3.3.1 Prefix Increment and Decrement Operators

The value resulting from a ++ or -- operation has been used.

The ++ and -- operators are special in that they don't just return a value, they also generate side effects - by modifying their operand. If the result value is used, there are some particular dangers:
  1. The different values resulting from the prefix version and the postfix version can cause confusion.
  2. The statement will generate multiple side effects which may be dangerous if the order of the side effects happens to be significant.

Code is generally safer and more readable if ++ and -- are used on their own.

For example:


/*PRQA S 506,2017,3199,3203,3227,3408,3447 ++*/


extern void f1(int p);

extern void foo(int n, int *p)
{
    int r;

    ++n;                                /* OK */

    (*p)--;                             /* OK */

    r = ++n;                            /* Message 3440 */

    f1(r++);                            /* Message 3440 and 3441 */

}


MISRA-C:2004 Rules applicable to message 3440:

Rule  12.13  (Advisory) The increment (++) and decrement (--) operators should not be mixed with other operators in an expression.


See also:

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