What does [i] = * (a_mat + i) do in C?
2 answers
In C, x[i]
is the same expression as *(x + i)
, because adding an integer to a pointer is done by scaling the integer to the size of the pointer object and because it is defined that way.
This means that, despite its asymmetric form, the indexing operator []
in C is commutative.
A traditional demo of this goes something like this:
main() {
int x[] = { 1, 2, 3, 4 };
printf("%d\n", x[2]);
printf("%d\n", 2[x]);
}
Both lines are equivalent and print the same thing.
+3
source to share