|
|
|
|
|
|
|
It may be more trouble than it is worth to thoroughly test this program using the procedure outlined above. If the program is to be used only once, three short files with values might suffice. If this program is to be used more than once, however, it should be tested rigorously. |
|
|
|
|
|
|
|
|
Before leaving this problem, we should talk about efficiency. When we input the data from a file into an array, the program stores the sentinel record into the array as well. Every loop that processes the array checks for the end of the list by invoking the strcmp library function. If each input file contains 100 people's records, the strcmp function is called hundreds of times. This is very time-consuming as strcmp itself uses a loop to compare characters in two strings. It would make more sense for the GetRecords function to return the integer length of the list instead of storing the sentinel record into the array. Thereafter, each loop could use the length of the list to control the number of loop iterations. |
|
|
|
|
|
|
|
|
If this program were to be run several times, it would be worthwhile to recode it so that function GetRecords returns the length of a list and the other functions make use of the appropriate list lengths. Case Study Follow-Up Exercise 3 asks you to do this. |
|
|
|
|
|
|
|
|
As we have demonstrated in many examples in this chapter and the last, it is possible to combine data structures in various ways: structs whose components are arrays, structs whose components are structs, arrays whose components are arrays (multidimensional arrays), and arrays whose components are structs. When arrays and structs are combined, there can be confusion about precisely where to place the operators for struct member selection (.) and array element selection ([ ]). |
|
|
|
|
|
|
|
|
To summarize the correct placement of these operators, let's use the StudentRec type we introduced at the beginning of the chapter: |
|
|
|
|
|
|
|
|
typedef char NameString[16];
struct StudentRec
{
NameString firstName;
NameString lastName;
float gpa;
.
.
.
}; |
|
|
|
|
|