Next: Arrays and pointers Up: Arrays and Pointers Previous: Arrays and Pointers

One-dimensional arrays

The syntax for declaring an array is:

   type name[size];

which says there are size elements (always numbered from zero upwards) of type type and the array is called name. For example:

   int digit[10];
   char letter[4];
   double frequency[100];

This causes adequate consecutive storage to be created and associated with the name.

Any item within the array can be referenced with

   name[some_integer_expression]

such as:

   name[3]
   name[i]
   name[3*i + 1]
etc

C does not check array bounds, so in the above the compiler will accept

   digit[20];

and this may or may not generate a run-time error. If it does not, it will reference the 20th item from the start of the array in memory. If it does, it will probably be because that item is beyond the memory allocated to you (Segmentation fault). Similar comments apply to:

   digit[-3];



Next: Arrays and pointers Up: Arrays and Pointers Previous: Arrays and Pointers


maspjw@
Tue Sep 27 15:29:34 BST 1994