Compound Data Types






Up to this point, all the variables we've seen can contain only a singl value. C also supports a number of different kinds of compound data structures, or those that can hold multiple pieces of data. The simplest of these, and the subject of this section, is the array.

Arrays

An array is essentially a list of data. Arrays can be of any type you want; however, they can contain only one type of data per array. You cannot, for example, have an array of both integers and floating point numbers--you must use a separate array for each. You declare an array in C much like you declare any other variable, with one slight twist. When you declare the array, you must also declare how big you want it to be, and this number goes in parentheses after the name, as below:

int intarray[10];

This declaration reserves enough space for ten integers that will all be stored in the intarray[] array. It is important to remember, though, that just like any other variable, the slots of a newly created array are not initialized to anything in particular, so if your program depends on them having a particular value you must first initialize them. A loop is a good way to do this, as in the loop below:

#define SIZE 10

int i, intarray[SIZE];

for (i = 0; i < SIZE; i++)
   intarray[i] = 0;

Notice that I snuck something else tricky into here, in brackets after the array. Each slot of the array is assigned a number so that you can access it later. These numbers start from 0, not from 1, which is something you should always remember. These are called the index numbers of the array. Since the numbers start from 0, they go up to 1 minus the size of the array. That is why the for loop above only goes up to 9 (since the condition is strictly less than). You must be careful not to go outside the boundary of an array, or else you'll get errors. You can then access any piece of the array by putting the array name followed by the desired index number in square brackets. The entire expression then evaluates to whatever is in that slot of the array. You can even put another expression in between the brackets, as long as it will evaluate to an integer (there are no fractional indexes!).

Functions and Arrays

Just as you can pass any scalar (i.e., non-array) variable to a function, you can also pass an array variable. The only difference is that in order to let the compiler know that you're passing an array variable, you must include a set of empty square brackets after the name. The compiler will calculate the size automatically. Passing arrays, though, is different from passing scalar types in one important way: when you pass a scalar variable to a function, the compiler allocates new memory and copies the value of the variable into the new space. That is why no changes you make to a variable inside a function are saved unless you explicitly return the value and set some variable equal to it. Copying the entire list of data from an array, though, would be highly inefficient. Instead, passing an array is called "passing by reference." What the program gives to the function is the address in memory where the array is stored (we'll get into memory more in the next section). What this means is that changes made to an array inside a function are preserved when the function exits. This is one way you can sneak around the fact that a function can only explicitly return one value.



Back to Section 2: More Abstraction   On to Memory and Pointers
Back to the Outline