When an expression refers to an object of array type, it can produce a pointer to the first element of the array. This behavior is known as
array-to-pointer decay or simply array decay.
Because of array-to-pointer decay, it is possible to use the dereference operator (*
) or the pointer member access operator
(->
) on an array object to access its first element. However, it remains unclear if accessing the first element was intentional. In
contrast, using array subscript ([0]
) removes any such ambiguity.
This rule raises an issue when the deference operator or the member access operator is used on an array.
struct Struct { int x; };
Struct elems[10];
int integers[5];
void init() {
for (int i = 0; i < 5; ++i) {
*integers = i; // Noncompliant: "*" used to access "integers[0]"
}
}
void process() {
for (int i = 0; i < 10; ++i) {
elems->x *= 2; // Noncompliant: "->" used to access "elems[0]"
}
}