Declaring Arrays

Declaring arrays is not much different from declaring simple data types; you just append a constant expression enclosed in square brackets:
  type name[size];
type is the data type of the array and can be any built-in or user defined type.
name is the name that you give to the array.
size is a constant expression indicating the number of elements in the array.

Examples:
const int MAX = 10;

int age[100];       // 100 integers, 0 - 99
char letter[15];    // 15 chars, 0 - 14
float weight[MAX];  // MAX floats, 0 - (MAX-1) 
long cost[3 * MAX]; // 3 * MAX longs, 0 - 29
Notice that the size of the array is a constant expression. This cannot be a variable. The size of the array must be known at compile time. These are illegal array declarations that the compiler would complain about:
int a, b = 10;
int age[a];       // illegal, a is not constant
float weight[b];  // illegal, b is not constant
Previous page
Next page

Back to Lesson 11 Index
Back to Outline