Skip to main content

IAR Embedded Workbench for RISC-V 3.40

CERT-ARR37-C

In this section:
Synopsis

Do not add or subtract an integer to a pointer to a non-array object.

Enabled by default

Yes

Severity/Certainty

Medium/Medium

mediummedium.png
Full description

Pointer arithmetic must be performed only on pointers that reference elements of array objects.

Coding standards
CERT ARR37-C

Do not add or subtract an integer to a pointer to a non-array object

Code examples

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

struct numbers {
    short num_a, num_b, num_c;
};

int sum_numbers(const struct numbers *numb){
    int total = 0;
    const short *numb_ptr;

    for (numb_ptr = &numb->num_a;
            numb_ptr <= &numb->num_c;
            numb_ptr++) {
        total += *(numb_ptr);
    }

    return total;
}

int main(void) {
    struct numbers my_numbers = { 1, 2, 3 };
    sum_numbers(&my_numbers);
    return 0;
}

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

struct numbers {
  short num_a, num_b, num_c;
};
void example(const struct numbers *numb) {
  int total = numb->num_a + numb->num_b + numb->num_c;
}