A Matter of Record
The concept of the array is a powerful one.
However, it is a limited one in the sense that we can only have
arrays of a single type of data.
That is we can't have an array where the first element is a
string, the next is an integer, the third is another string and
the fourth is a floating point number.
So we can't use an array to store the information about a
student, for example.
It would be nice if we could have a representation of data
we could call a student which had items like these:
-
The student's name - a string (array of characters)
-
The student's home address - a string
-
The student's campus extension - an integer
-
The student's expected year of graduation - an integer
-
The student's current GPA - a floating point number
Of course, others could be added, but these are enough to
illustrate what we're interested in.
Many programming languages all the way back to one of the first
(i.e. COBOL) include the ability to this.
In general, we call such collections of data
records
.
The advantage of records is that we can treat complex data
in dual ways.
First we can look at the individual components of that data,
and on the other hand, we can treat it as a single, named
entity.
Another difference we generally find between arrays and
records has to do with how we reference parts of them.
For arrays, in most languages, we must refer to the
element of interest by an integer number, but with records
we usually give the elements names.
(Remember, though, that in C we can define symbolic constants
with the
#define
directive that can give names
to any numbers including integers.)
This will often win us some additional clarity in our code.