A postfix expression followed by an expression in [ ] (brackets) specifies an element of an array. The expression within the brackets is referred to as a subscript. The first element of an array has the subscript zero.
#include <stdio.h>
int main(void) {
int a[3] = { 10, 20, 30 };
printf("a[0] = %d\n", a[0]);
printf("a[1] = %d\n", 1[a]);
printf("a[2] = %d\n", *(2 + a));
return 0;
}
See the output of the above example: a[0] = 10
a[1] = 20
a[2] = 30
The above restrictions on the types of expressions required by the subscript operator, as well as the relationship between the subscript operator and pointer arithmetic, do not apply if you overload operator[] of a class.
The first element of each array has the subscript 0. The expression contract[35] refers to the 36th element in the array contract.
In a multidimensional array, you can reference each element (in the order of increasing storage locations) by incrementing the right-most subscript most frequently.
for (first = 0; first < 4; ++first)
{
for (second = 0; second < 3; ++second)
{
for (third = 0; third < 6; ++third)
{
code[first][second][third] =
100;
}
}
}
struct trio{int a[3];};
struct trio f();
foo (int index)
{
return f().a[index];
}