CS1B Saddleback College Related items different datatypes Before we used parallel arrays CS1B Introduction to Programming in C there is a better way Topic 7 Structs records Structs a k a records Allow us to store different types of related data in one structure Member Chapter 11 in the shrinkwrap Is a field within a structure Basic Process 1 Define the struct 2 Declare an identifier as of type struct 3 Use the struct 2 CS1B Saddleback College Topic 7 Structs CS1B Saddleback College 1 Defining a Struct 2 Declaring a Struct Syntax struct structName datatype memberName Now you can use your struct as you would any other userdefined datatype This is similar to how we used the typedef for our arrays Note You need this ending semi colon To declare a variable of our new struct type StudentRec Example Declare it just like any other identifier struct StudentRec string name int idNum in the declaration section of your program use the datatype StudentRec Put this prior to declaring a variable of this type Just like you would an enum or typedef StudentRec theStudent float gpa 3 Topic 7 Structs 4 Topic 7 Structs CS1B Saddleback College CS1B Saddleback College 3 Using a Struct Assigning values to a struct Now we want to access different members within our variable theStudent We can also assign values just like any other datatype Remember theStudent has the following members theStudent string name int idNum float gpa theStudent name Joe Smith name idNum gpa theStudent idNum 1003 theStudent gpa 2 35 To access these variables we use the dot operator We can compare them too Syntax variableName memberName if theStudent gpa 2 0 Example theStudent name cout theStudent name is passing Now we can access each member just as we would any other variable cout What is the student s name We refer to our struct now by the variable name theStudent NOT the struct name StudentRec getline cin theStudent name Topic 7 Structs 5 6 CS1B Saddleback College CS1B Saddleback College Putting it all together include myHeader h int main struct StudentRec string name int idNum float gpa StudentRec theStudent Using structs in Arrays Declare an array of structs just like any other variable Defines our struct we now can use StudentRec as a datatype StudentRec students 25 These are StudentRec s members Treat them just like any other array Declares theStudent as our struct type students 2 name Joe Smith cout Enter the student s name getline cin theStudent name Note the index goes after the struct variable not the member cout Enter the student s ID cin theStudent idNum cout Enter the student s GPA cin theStudent gpa cout cout cout cout cout We use the operator to access the members for this variable 0 name idNum gpa endl endl theStudent name s id is theStudent idNum endl theStudent name s GPA is theStudent gpa endl 1 Joe Smith 1003 2 35 name idNum gpa 2 Jane Doe 1004 3 75 name idNum gpa 7 Joe Smith 1003 2 35 Topic 7 Structs 8 Topic 7 Structs Frank Smith 1293 3 01 CS1B Saddleback College CS1B Saddleback College Comparing an array of structs Structs Dos and Dont s int findJoe StudentRec students int size Aggregate I O not allowed must specify members Can t do this cout theStudent int foundHere Aggregate arithmetic is not allowed Can t do this theStudent theStudent 1 foundHere 1 Aggregate comparison is not allowed for int index 0 index size index Can t do this if theStudent anotherStudent if students index name Joe Smith Can do this if theStudent name anotherStudent name foundHere index structs CAN be passed by value or by reference structs CAN be a return type in a function Aggregate assignment is allowed return foundHere 9 Can do this theStudent anotherStudent Is this an efficient search Topic 7 Structs ALL members are copied to corresponding locations 10 Topic 7 Structs
View Full Document