while( i < a_rows * a_cols ) {
out[i] = *(a_mat + i); // this line
i++;
}
What does the marked line do?
while( i < a_rows * a_cols ) {
out[i] = *(a_mat + i); // this line
i++;
}
What does the marked line do?
It gets the value of whatever is pointed to by a_mat + i
. It could have been written a_mat[i]
instead.
In C, x[i]
is the same expression as *(x + i)
, because adding an integer to a pointer is done by scaling the integer by the size of the pointed-to object, and because it's defined that way.
This means that despite its asymmetrical appearance, the indexing operator []
in C is commutative.
The traditional demonstration of this is something like:
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.