Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The C99 definition of the % operator implies the following behavior:

Code Block
 17 %  3  ->  2
 17 % -3  ->  2
-17 %  3  -> -2
-17 % -3  -> -2

...

This noncompliant code example also violates INT01-C. Use rsize_t or size_t for all integer values representing the size of an object.

Compliant Solution

...

(absolute value)

This compliant solution calculates the absolute value of the modulo operation.

Code Block
bgColor#FFCCCC

int insert(int index, int *list, int size, int value

To provide a nonnegative modulo operation, use the imod() ("integer modulo") inline function:

Code Block

/* modulo function giving non-negative result */
inline int imod(int i, int j) {
  returnif (isize %!= j0) {
   < 0index ?= (iabs((index + 1) % j) + (j < 0 ? -j : j) : i % j;
}
size);
    list[index] = value;
    return index;
  }
  else {
    return -1;
  }
}

Compliant Solution (unsigned types)

However, the most appropriate solution in this case is to use unsigned types as in this compliant solution:

...