Usually, the array size is fixed, while strings can have a variable number of elements.
Arrays can contain any data type (char short int even other arrays) while strings are usually ASCII characters terminated with a NULL (0) character.
Another situation in which an array's size need not be specified is when the array elements are given initial values.
As we will see in Chapter 9, the compiler will determine the size of such an array from the number of initial values. Most obviously, we can write subscripted references to array elements, as we have already seen.
Multi-dimensional arrays require multiple sets of brackets.
The examples in Listing 8-1 are valid declarations.short data; /* define data, allocate space for 5 16-bit integers */ char string; /* define string, allocate space for 20 8-bit characters */ int time,width; /* define time, width, allocate space for 16-bit characters */ short xx; /* define xx, allocate space for 50 16-bit integers */ short pts; /* define pts, allocate space for 125 16-bit integers */ extern char buffer; /* declare buffer as an external character array */Notice in the third example that ordinary variables may be declared together with arrays in the same statement.
Since these differences are a matter of semantics rather than specific limitations imposed by the syntax of the C programming language, the descriptions in this chapter apply equally to data arrays and character strings.
String literals were discussed earlier in Chapter 3; in this chapter we will define data structures to hold our strings.
In general we allow random access to individual array elements.
On the other hand, we usually process strings sequentially character by character from start to end.