Array subscript expression written with operands reversed
ID |
c.maintainability.reversed_array_index |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Readability |
Language |
C / C++ |
Description
This subscript expression puts the integer literal on the left and the array on the right (N[array]). It compiles and behaves identically to array[N] because a[b] is defined as *(a + b), which is commutative, but it reads as a mistake to anyone unfamiliar with that trivia. Write it as array[N].
Rationale
This subscript expression puts the integer literal on the left and the array on the right (N[array]). It compiles and behaves identically to array[N] because a[b] is defined as *(a + b), which is commutative, but it reads as a mistake to anyone unfamiliar with that trivia. Write it as array[N].
The following code illustrates the pattern detected by this rule:
void reversed(int arr[10])
{
// FLAGGED: Array subscript expression written with operands reversed
int a = 3[arr];
(void)a;
}