Type of array index in C++

后端 未结 4 1336
甜味超标
甜味超标 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: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]
    

提交回复
热议问题