24

What is the type of array index in C++ programming language? For example in such statement:

int tab[5];

To what type 5 is converted? or maybe is it just plain int?

5
  • 10
    I'm just curious how this information could be useful to you. Commented Nov 13, 2011 at 12:02
  • 20
    Maybe he's curious too? Commented Nov 13, 2011 at 12:59
  • 1
    Related for C for array access: stackoverflow.com/questions/3174850/type-for-array-index-in-c99 Commented Jun 27, 2015 at 13:55
  • 1
    It would be nice if there would be a std::index_t like there is already a std::size_t Commented Aug 29, 2017 at 14:52
  • @feedc0de There's gsl::index which have a good chance of joining the standard library isocpp.github.io/CppCoreGuidelines/… Commented Mar 28, 2021 at 23:11

4 Answers 4

16

In that code, 5 is just a plain integer literal, so it's just a plain int here.

§8.3.4 Arrays in n3290 (~ C++11) specifies array declarators:

In a declaration T D where D has the form

D1 [ constant-expressionopt ] attribute-specifier-seqopt

and the type of the identifier in the declaration T D1 is “derived-declarator-type-list T”, then the type of the identifier of D is an array type; if the type of the identifier of D contains the auto type-specifier, the program is ill-formed. T is called the array element type; this type shall not be a reference type, the (possibly cv-qualified) type void, a function type or an abstract class type. If the constant-expression (5.19) is present,it shall be an integral constant expression and its value shall be greater than zero.

§5.2.1 Subscripting describes what can go in the brackets in expressions:

A postfix expression followed by an expression in square brackets is a postfix expression. One of the expressions shall have the type “pointer to T” and the other shall have unscoped enumeration or integral type. The result is an lvalue of type “T.” The type “T” shall be a completely-defined object type. The expression E1[E2] is identical (by definition) to *((E1)+(E2))

Sign up to request clarification or add additional context in comments.

3 Comments

Wrong chapter. He asks about the declaration and you quote the subscript expression description. That's different because for his case there are no negative integer values allowed. So it isn't obvious whether the same constraints apply.
@JohannesSchaub-litb: added the relevant part for the declarator. I think the question wasn't specifically about the declaration part, and that's why I put the subscripting section as a reference.
@Johannes Schaub - litb: To be fair the title is Type of array index in C++. I've covered the array declaration part in my answer.
6

The question is somewhat confusing. The title mentions Type of array index, but in the question, you seem to ask something else. Are you asking about size of an array? or index to an array? The size of a declared array must be greater than zero; it can any integral type: int, char, signed char, unsigned int, and so on. In your question, the type of literal 5 is int.

However, if you're asking about type of index to an array, then it must be one of the integral type. The index type to an array can be int also, as it can even be negative.

int a[10][10];

int x = a[3][-1]; //same as a[2][9]
int y = a[3][-2]; //same as a[2][8]
int z = a[3][-3]; //same as a[2][7]

5 Comments

c++11 standard (draft n3242) §8.3.4 says that the value shall be greater than zero.
@J.F.Sebastian: If the value shall be greater than zero, then it means it is illegal to write a[0][0]? I think you are confusing index to an array with the size of an array. It is the size which must be greater than zero!
"The size of an array must be greater than zero"- this is not true really. Only for declared arrays it is true. For example this is fine: new int[0].
@Nawaz: The code in the question as you've noticed is an array declaration. I thought you're referring to possible values instead of 5. You're right I've confused an index and size of an array.
@JohannesSchaub-litb: With that I agree. But the question mentions declared array, so I answered only that part. A more detailed answer might include that the type can be anything if it is user-defined class which overloads operator[].
5

int tab[5]; is an array declaration.

Array declaration accepts an integral constant expression that has value greater than zero (c++11: §8.3.4).

§5.19.4 (n3242):

A constant expression is an integral constant expression if it is of integral or enumeration type. [ Note: Such expressions may be used as array bounds (8.3.4, 5.3.4), as case expressions (6.4.2), as bit-field lengths (9.6), ...


5 is an integer literal (§2.14.2). Its type is int.

§2.14.2 (n3242)

2 The type of an integer literal is the first of the corresponding list in Table 6 in which its value can be represented.

3 If an integer literal cannot be represented by any type in its list and an extended integer type (3.9.1) can represent its value, it may have that extended integer type. ...

Types of decimal constants without suffix in the Table 6 are: int, long int, long long int.

Comments

3

This is int if you wanta different type use sufix, for example:

5         // int
5u        // unsigned int
5l        // long
5ul       // unsigned long 

1 Comment

note: an integer literal without a suffix can be int, long int, long long int or may have an extended integer type. See my answer e.g., 5000000000 is long on my machine and it is long long on ideone.com.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.