Type of array index in C++

后端 未结 4 1337
甜味超标
甜味超标 2020-12-06 16:35

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 may

相关标签:
4条回答
  • 2020-12-06 16:38

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

    5         // int
    5u        // unsigned int
    5l        // long
    5ul       // unsigned long 
    
    0 讨论(0)
  • 2020-12-06 16:41

    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]
    
    0 讨论(0)
  • 2020-12-06 16:56

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

    0 讨论(0)
  • 2020-12-06 17:02

    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.

    0 讨论(0)
提交回复
热议问题