11/11/05 CISC105 Lecture Notes (1) Defining a struct (1a) One reason to do it is so we can avoid parallel arrays. With an array of structs, we can represent a table with any number of columns, using only a single array. Each element in the array will be a "struct" that contains several fields. (The fields are also called "members of the struct") For example: #define TEAM_NAME_SIZE 32 struct PackerStats_S { char teamName[TEAM_NAME_SIZE]; int pointsScored; int pointsAgainst; }; This struct contains three fields: teamName pointsScored pointsAgainst If I declare a single variable: PackerStats_S homeOpener; Now I have a variable: homeOpener +---------------------------------------------+ | +----------------------------+ | . teamName |Bears | | | +----------------------------+ | | +---------------+ | . pointsScored |30 | | | +---------------+ | | +---------------+ | . pointsAgainst|50 | | | +---------------+ | +---------------------------------------------+ I can access the fields in this struct by using variables such as: strncpy(homeOpener.teamName,"Bears",TEAM_NAME_SIZE); homeOpener.pointsScored = 30; homeOpener.pointsAgainst = 50; printf("The opponent for the home opener was %s\n", homeOpener.teamName); if (homeOpener.pointsScored < homeOpener.pointsAgainst) printf("The Packers lost, %d to %d\n", homeOpener.pointsScored, homeOpener.pointsAgainst ); else if (homeOpener.pointsScored > homeOpener.pointsAgainst) printf("The Packers won, %d to %d\n",homeOpener.pointsScored, homeOpener.pointsAgainst ); else /* they are equal */ printf("It was a tie game, %d to %d\n", homeOpener.pointsScored, homeOpener.pointsAgainst ); If I declare an array: #define MAX_NUM_GAMES 20 PackerStats_S greenBayStats[MAX_NUM_GAMES]; This array means that we have 20 elements, each of which contains teamName pointsScored pointsAgainst So it looks like this in memory: greenBayStats[0] +---------------------------------------------+ | +----------------------------+ | . teamName | | | | +----------------------------+ | | +---------------+ | . pointsScored | | | | +---------------+ | | +---------------+ | . pointsAgainst| | | | +---------------+ | +---------------------------------------------+ greenBayStats[1] +---------------------------------------------+ | +----------------------------+ | . teamName | | | | +----------------------------+ | | +---------------+ | . pointsScored | | | | +---------------+ | | +---------------+ | . pointsAgainst| | | | +---------------+ | +---------------------------------------------+ . . . greenBayStats[19] +---------------------------------------------+ | +----------------------------+ | . teamName | | | | +----------------------------+ | | +---------------+ | . pointsScored | | | | +---------------+ | | +---------------+ | . pointsAgainst| | | | +---------------+ | +---------------------------------------------+ If I want to assign the first element in this struct to contain: Panthers,20,23 strncpy(greenBayStats[0].teamName,"Panthers",TEAM_NAME_SIZE); greenBayStats[0].pointsScored = 20; greenBayStats[0].pointsAgainst = 23; We can't just do: greenBayStats[0].teamName = "Panthers"; because teamName is an array of characters, and you can't copy an entire array with an assignment statement. (2) Why the _S (3) A note about naming...