Arrays must be indexed by int values; short, byte, or char values may also be used as index values because they are subjected to unary numeric promotion (§5.6.1) and become int values. An attempt to access an array component with a long index value results in a compile-time error.
Ernest Friedman-Hill wrote:Well, it has to be a countable set of symbols, because the index refers to the individual discrete cells of an array, ordered from one end to the other. If floating point numbers were allowed, how would you handle the fractional part?
Campbell Ritchie wrote:I suspect using ints dates back to the time of C when you found the addressable location by adding the index to the location of the array. It is awkward to find the next member by adding a floating-point number, but very fast by adding an integer. You can do it at the chip level with relative addressing.
I presume they chose int rather than long because they thought nobody would need an array of ≥ 2^31 elements. Or even to prohibit anybody using an array with 2^31 elements.
Sebastian Janisch wrote:Why int and not long.
The designers probably thought that an Array will never exceed 2,147,483,647 elements.