3 ms·
Yes, that works because for an array arr and an int i, arr[i] (i'th element of array arr) is the same as *(arr + i) (the value at the memory address arr
by vram22 8y ago
Yes, that works because for an array arr and an int i,
arr[i] (i'th element of array arr)
is the same as
*(arr + i) (the value at the memory address arr + i)
which is the same (by commutativity) as
*(i + arr) (the value at the memory address i + arr)
which in turn is the same as
i[arr]
That last bit is what seems non-intuitive, but it is true.
I first read about this, maybe in the K&R book or some other C book, many years ago, at first didn't believe it, and remember trying it out on at least a Windows C compiler (MS C, likely) and maybe GCC on Linux as well. Worked on both, no compiler error.
This works because the expression arr, while we think of it as the name of the declared array, is also a synonym for the address of the first memory location of the array.
Note: all that I wrote above is what I rememeber from using ANSI C (a lot, but some years earlier). Things may have changed some with C99, etc.
- vram22 8y ago>rememeber remember