< previous page page_823 next page >

Page 823
The index [0] is correctly attached to the identifier gradeBook because gradeBook is the name of an array. Furthermore, the expression
gradeBook[0]
denotes a struct, so the dot operator selects the gpa member of this struct.
Another potential source of confusion when working with structs is the scope of member names. We said earlier that member names must be unique within a struct. Additionally, we mentioned in Chapter 8 that there are three kinds of scope in C++: local scope, file (global) scope, and class scope. Class scope applies to the member names within structs, unions, and classes (the topic of Chapter 15). To say that a struct member name has class scope means that the name is local to that struct. If the same identifier happens to be declared outside the struct, the two identifiers are unrelated. For example, name is such a meaningful identifier that it might be used logically in more than one struct. Look at the following declarations:
const int CLASS_SIZE = 120;
const int NUM_CITIES = 1000;

typedef char String20[21];

enum YearType {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR};

struct PersonType

{
    String20 name;
    YearType year;
    int      age;
};
struct CityType
{
    String20 name;
    String20 state;
    int      elevation;

};

PersonType oneStudent;
String20   name;
PersonType roster[CLASS_SIZE];
CityType   city[NUM_CITIES];
The declarations are valid, even though the identifier name is declared in three places: as a string variable, a member name in the PersonType struct, and a member name in the CityType struct. No ambiguities arise because name, oneStudent.name, roster[i].name, and city[i].name are all unique. No ambiguities arise for the compiler, that is; but ambiguities can arise for

 
< previous page page_823 next page >