type identifier[const int expression]
- int ages[20];
- float conversionFactors[LAST_PLANET];
- PersonT travelers[MAX_PASSENGERS];
NOTE the constant expression.
THIS IS NOT LEGAL
int i;
cout << "Enter the number of people"
cin >> i;
PersonT people[i];
Some compilers will allow this, some will even do it right!
Initialization of an array at declaration is possible.
- string names[] = {"Albus","Wulfric", "Brian", "Dumbledore"};
- PlanetT destinations[] = {MARS, VENUS, Earth};
- int primes[5]={2,3,5,7,11};
- Note, this last version is not preferred, as it can lead to errors.
- sizeof operator is probably useful here.
Accessing elements
- Elements are at locations 0 through size-1
- So if we have int foo[10], there are 10 elements at positions 0 through 9
- foo[3]=5 will assign a five to the fourth element of the array (at position 3)
- y = foo[7] * 3 + 1 will take the value at position 7, multiply it by 3 add one and store the result in y.
- foo[-1] and foo[10] are both array index out of bounds errors
- The compiler does not catch these.
- Nor does runtime, for quite a while.
- segmentation fault
Aggregate operations
I/O | NO |
Assignment | NO |
Math | NO |
Comparison | NO |
Argument | By Reference Only |
Return | NO |