scdmb
scdmb

Reputation: 15621

Type of array index in C++

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?

Upvotes: 24

Views: 19867

Answers (4)

jfs
jfs

Reputation: 414207

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.

Upvotes: 5

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361402

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]

Upvotes: 6

Mat
Mat

Reputation: 206689

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))

Upvotes: 16

Michał Powaga
Michał Powaga

Reputation: 23183

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

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

Upvotes: 3

Related Questions