4 ms·
C really does stress the difference. Look up the definitions of "pointer" and "array" in the C standard. They're entirely distinct concepts. p[i] is equivalent
by _kst_ 8y ago
C really does stress the difference. Look up the definitions of "pointer" and "array" in the C standard. They're entirely distinct concepts.
p[i] is equivalent to (p+i), and that's possible because both the indexing "[]" operator and pointer addition "+" operator require a pointer and an integer as operands. (The pointer needs to point to an element of an array object, but that's not enforced at compile time.) For p[i], it's very common for the pointer operand to be an array expression that "decays" to a pointer.
(3[p] is valid because pointer addition, like integer and floating-point addition, is commutative. It could* have been defined to require a pointer as the left operand and an integer as the right operand. The decision was made when the distinction between integers and pointers wasn't as strong as it is in modern C.)