4 ms·
Fun read :) I'm not sure if this is correct: > By elements we mean the size of the type of the array. If an array holds elements of type int which each occupy
by RegEx 14y ago
Fun read :)
I'm not sure if this is correct:
> By elements we mean the size of the type of the array. If an array holds elements of type int which each occupy four bytes, then integer_array[5000] is the same as
*(integer_array + (5000 * 4)).
And that directly contradicts the earlier statement
>with subscripts e.g. my_array[271] would give us the 270th element. This by the way, would do exactly the same thing as
*(my_array + 271).
(which also may be incorrect: my_array[271] would give you the 272nd element if you count my_array[0] as the "first" and 271st element if you count my_array[0] as the "0th".)
I dug up the relevant part in the C standard just to be sure:
> The definition of the subscript operator [] is that E1[E2] is identical to
(*((E1)+(E2))).
- greenyoda 14y agoYou're right, the article is wrong. If integer_array is an array of ints or a pointer to an int, then integer_array[5000] is equivalent to *(integer_array + 5000), not *(integer_array + (5000 * 4)), since pointer arithmetic in C automatically adjusts for the size of an array element.
- alinajaf 14y agoThanks for pointing this out, will fix!