Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type of array index in C++

Tags:

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?

like image 688
scdmb Avatar asked Nov 13 '11 12:11

scdmb


People also ask

What is the type of an array index?

An array is an indexed collection of component variables, called the elements of the array. The indexes are the values of an ordinal type, called the index type of the array. The elements all have the same size and the same type, called the element type of the array. There are two kinds of array types, fixed and open.

What type is an array index in C?

The array index is an integer number and its size should correspond to the maximum size of a pointer variable on your platform. Since most platforms have fixed size pointers and in C their sizes are characterized by the type long , then I suppose it is fair to say that array indices are of type long .

What are the 3 types of arrays?

There are three different kinds of arrays: indexed arrays, multidimensional arrays, and associative arrays.

What are types of array in C programming?

Array in C are of two types; Single dimensional arrays and Multidimensional arrays.


2 Answers

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

like image 155
Mat Avatar answered Sep 29 '22 23:09

Mat


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] 
like image 25
Nawaz Avatar answered Sep 30 '22 00:09

Nawaz