Chapter 11, Arrays
Read Chapter 11.1 and 11.2
Caution: Arrays are old
Part of C.
They behave differently from most other data structures.
Do not generalize based on arrays.
Caution: Arrays are dangerous
With arrays we will find brave new ways to crash your program.
Do not have array index out of bounds errors.
Arrays are designed to hold a collection of homogeneous information.
Arrays should be used any time you feel compelled to name variables value1, value2, value3 ... valuen
Syntax
type identifier[const int expression]
const int expression is the size of the array
Examples
int numbers[10]; ColorT colors[MAX_COLORS]; PlayerT players[MAX_PLAYERS];
Caution:
The size of the array
Must be positive.
Must be known at compile time
Is usually declared as a constant.
Accessing elements
An array with n elements is accessed via an index i, where 0 ≤ i < n
So an array with 10 elements is accessed with the index values 0 ... 9 inclusive.
Using a value less than 0, or greater or equal to the array size is an
array index out of bounds error
.
Accessing an array with an out of bounds index error
May or may not cause a run time error
Will almost certainly result in erroneous data.
You can not do most operations directly on arrays
Look at page 524 for a table.
But the only real operation is pass by value
And that is a lie, but it will work.
Generate an array of players for Cthulhu Dice
Print out the players for Cthulhu Dice