Arrays

An array is a collection of variables having the same data type. The size of the array is fixed when it is first declared.

Declaration

Example This declares an array called myarray with 10 elements of type integer.

Indexing the array

Syntax This is how you put information in and out of the array. To access a particular element in the array, you have to give an index. The index is the slot that points to a particular spot on the array. The index can be a constant such as 3, 256, or 2. It can also be a variable or an expression that equals some valid array index.

A valid index is a value between 0 and 1 minus the size of the array. That is, since myarray is of size 10, the valid indexes are 0-9. 0 is the first spot, and 9 is the last spot. If you tried to access 10 you would be going beyond the length of the array and will probably get a segmentation fault (these are BAD bugs and often difficult to find).

Example

The second line assigns 10 to the element in myarray position (slot) 5 (the 6th element). The third line assigns the element at myarray position 5 to b. b now has the value 10.