Skip to main content

IAR Embedded Workbench for RH850 3.20.x

MISRAC++2008-7-1-2

In this section:
Synopsis

(Required) A pointer or reference parameter in a function shall be declared as pointer to const or reference to const if the corresponding object is not modified.

Enabled by default

Yes

Severity/Certainty

Low/Medium

lowmedium.png
Full description

A parameter in a function that is not modified by the function is not const qualified. This check is identical to MISRAC2004-16.7, MISRAC++2023-10.1.1.

Coding standards
MISRA C:2004 16.7

(Required) A pointer parameter in a function prototype should be declared as pointer to const if the pointer is not used to modify the addressed object.

MISRA C++ 2023 10.1.1

(Advisory) The target type of a pointer or lvalue reference parameter should be const-qualified appropriately

Code examples

The following code example fails the check and will give a warning:

int example(int* x) {  //x should be const
  if (*x > 5){
    return *x;
  } else {
    return 5;
  }
}

The following code example passes the check and will not give a warning about this issue:

int example(const int* x) {  //OK
  if (*x > 5){
    return *x;
  } else {
    return 5;
  }
}