Previous: Chapter 7
Up: Table of Contents
Previous Page: Chapter 7
Next Page: A Compound Data Type --- array
A programmer is concerned with developing and implementing algorithms for a variety of tasks. As tasks become more complex, algorithm development is facilitated by structuring or organizing data in specialized ways. There is no best data structure for all tasks; suitable data structures must be selected for the specific task. Some data structures are provided by programming languages; others must be derived by the programmer from available data types and structures.
So far we have used integer, floating point and character data types as well as pointers to them. These data types are called base or scalar data types. Such base data types may be used to derive data structures which are organized groupings of instances of these types. The C language provides some widely used compound or derived data types together with mechanisms which allow the programmer to define variables of these types and access the data stored within them.
The first such type we will discuss is called an array. Many tasks require storing and processing a list of data items. For example, we may need to store a list of exam scores and to process it in numerous ways: find the maximum and minimum, average the scores, sort the scores in descending order, search for a specific score, etc. Data items in simple lists are usually of the same scalar type; for example a list of exam scores consists of all integer type items. We naturally think of a list as a data structure that should be referenced as a unit. C provides a derived data type that stores such a list of objects where each object is of the same data type - the array.
In this chapter, we will discuss arrays; how they are declared and data
is accessed in an array. We will discuss the relationship
between arrays and pointers
and how arrays are passed as arguments in function calls.
We will present several example programs using arrays, including a revision
of our ``payroll'' task from previous chapters.
One important
use of arrays is to hold strings of characters.
We will introduce strings in this chapter
and show how they are stored in C; however, since strings are important in
handling non-numeric data, we will discuss string processing at length in
Chapter .