Arrays are often used to implement other data structures, such as queues, stacks, lists and hash tables.
An array (in C) is a collection of individual data elements that is:
elementtype arrayname [ arraysize ] ;
Examples of array declarations:
|
FE09 |
|
|
|||||||||
In C, the elements of an array with N elements are indexed by the integers 0, 1, 2, 3, ... (N-1).
|
FE09 |
|
|
|||||||||
| 0 | 1 | 2 | 3 | 4 | |||||
arrayname [ indexValue ]
Examples:
|
FE09 |
|
|||||||||
| 0 | 1 | 2 | 3 | 4 | |||||
/* Initialization of all of the
elements of the array to 0 */
for (i = 0; i < 5; i++)
{
sample [i] = 0;
}
would cause all of the elements of the array to be set to 0
|
FE09 |
|
|||||||||
| 0 | 1 | 2 | 3 | 4 | |||||
If
data elements of an array are arrays themselves, it is called a multidimensional
array.
Although a two-dimensional array is really an array of arrays, it is more typical to think of it as a two dimensional table.
Multidimensional arrays are also commonly used. Up to three subscripts is not unusual. ANSI C requires that an implementation allow multidimensional arrays of at least six dimensions.