Coolwater
Coolwater

Reputation: 723

c++, do array indices need to be int?

In c++, a const array, arr, contains 100 numbers between 0 and 80.

If I choose the numbers in arr to be chars, will they be implicitly converted to int everytime they are used as indices on double-pointers, i.e. doublepointer[arr[i]]?

Upvotes: 4

Views: 3726

Answers (4)

Keith Thompson
Keith Thompson

Reputation: 263257

To answer the question in your title, an array index can be of any "unscoped enumeration or integral type". Array indexing is defined in terms of pointer addition; one operand must be a pointer to a completely-defined object type, and the other must be of some integral or unscoped enumeration type.

(Note that the word "object" here has nothing to do with object-oriented programming.)

There's nothing special about type int in this context. When you define an array type or object, you don't specify the type of the index, just the element type and the number of elements. When you use an index expression arr[i], the index can be of any integral type; for example unsigned int and long long are valid, and will not be implicitly converted.

To address the specific code you're asking about, char is an integral type, so it's perfectly valid as an array index -- but you need to be careful. The "usual arithmetic conversions" are applied to the index expression, which means that if it's of a type narrower than int it will be promoted to int or to unsigned int. These promotions do not apply to an index whose type is already at least as wide as int.

If plain char happens to be signed in your implementation, and if the value happens to be negative, then it will be promoted to a negative int value, which is probably not what you want. In your particular case, you say the values are between 0 to 80, all of which are within the range of positive values of type char. But just in case your requirements change later, you'd be better off defining your array with an element type of unsigned char.

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310970

Yes, they will be converted to type int. According to the C++ Standard "subscript operator [] is interpreted in such a way that E1[E2] is identical to *((E1)+(E2))." And if the additive operator is used then "The usual arithmetic conversions are performed for operands of arithmetic or enumeration type." This means that objects of type char will be converted to objects of type int when they are used in expressions as indices in the subscript operator.

Take into account that type char may behave either as unsigned char or as signed char depending on the compiler options you will select or that are set by default.

As for types that can be used as indices in the subscript operator then they shall be either unscoped enumerations or some integral types.

Upvotes: 5

For a genuine array, the index is (converted to) some integral type, as explained in Vlad's answer.

But several STL containers e.g. std::map or std::vector have their own operator [] whose argument might be (e.g. for some map-s) a non-integral type. By convention, that operator might be related to some at member function.

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172428

No it is not necessary that you can have an int as a index of an array. You can have characters as array index but they have there own problems. Char indexes can create problems as they may be either signed or unsigned, depending on the implementation. If a user-provided character is used as an array index, it's possible that the value is negative, and in most cases, that would mean memory outside of the array will be accessed. Hence it would result in an unnecessary chaos. Hence int is recommended and mostly used as array index

Upvotes: 0

Related Questions