Structure i e struct An structure creates a









![Array of Structs • Declaration of an array of structs S[0] student S[10]; • Array of Structs • Declaration of an array of structs S[0] student S[10]; •](https://slidetodoc.com/presentation_image_h2/dabfe4a0109ec7645d86ab2acfc0a7cc/image-10.jpg)
- Slides: 10
Structure (i. e. struct) • An structure creates a user defined data type • Contains a grouping of related data elements of arbitrary type and number • A “container” of other data elements • Individual components of the structure are called its members • Three parts to defining and using a structure • declaration of structure • use of structure to define a new variable • access of members of the structured variable Professor John Carelli Kutztown University Source: Pearson Education, Inc.
Structure declaration (generic) • Declare a new structure using the keyword struct • Provide a name for the new data type • List the members and their individual types Professor John Carelli struct stype { type 1 member 1; type 2 member 2; . . . type. N member. N; }; Kutztown University Computer Science Department
Structure declaration (example) struct student { string first. Name; string last. Name; int age; float GPA; char gender; int class; }; Professor John Carelli • Creates a new data type called student • Members include: • Strings to store first and last name • Integer for student’s age and class • Float for student’s grade point average • Char for student’s gender Kutztown University Computer Science Department
Structure usage • Variable declaration • Example: • Use the newly defined data type to create a variable as you would any other, built-in, data type • Access individual data members using the dot (. ) operator • variable. member • Use this as you would any other variable of the given type Professor John Carelli Kutztown University student S 1; // variable declaration! S 1. first. Name= “John”; S 1. last. Name= “Doe”; S 1. age= 20; S 1. gender= ‘m’; … // happy birthday! S 1. age+= 1; Computer Science Department
structs as Operands and Arguments • struct copy or assignment student S 2= S 1; // copy S 1 to S 2 • Copies each member from S 1 struct to S 2 struct Professor John Carelli Kutztown University Source: Pearson 5 Education, Inc.
Storage • Structure member elements are stored in sequential memory locations Student S 1 Memory Address Member Value 0 x 1 xxx first. Name* John 0 x 1 yyy last. Name* Doe 0 x 1100 age 20 0 x 1104 GPA 4. 0 0 x 1108 gender** m 0 x 1109 class 2 * size of a string ** may actually use more than one byte Professor John Carelli Kutztown University Computer Science Department
Passing a struct as a function argument • Argument must be of the same struct type as in the function definition/prototype • Using structs as arguments can shorten the argument list • Passing structs by value can be inefficient, since it duplicates values of all members • “pass by value” copies the entire structure Professor John Carelli Kutztown University 7 Source: Pearson Education, Inc.
Passing struct by Reference • Same as any other reference parameter • use & to identify in parameter list • void print. Student(student &s) • struct members can be modified • Can also use constant reference • precede parameter with reserved word const • E. g. void print. Student(const student &s) o const prevents the function from changing any member in the structure Professor John Carelli Kutztown University 8 Source: Pearson Education, Inc.
Function print. Student() void print. Student(const student &s) { cout << “Student Information: " << endl; cout << " Name : " << s. first. Name << “ “ << s. last. Name << endl; cout << " Age : " << s. age << endl; cout << " Gender : " << s. gender << endl; cout << " Class : " << s. class << endl; cout << " GPA : " << s. GPA << endl; } Example: General/struct. cpp Professor John Carelli Kutztown University 9 Pearson Education, Inc. Source:
Array of Structs • Declaration of an array of structs S[0] student S[10]; • Storage for 10 student structs • Can pass to a function print. Student(S[i]); S[1] • Storage is in sequential memory locations Professor John Carelli Kutztown University first. Name John last. Name Doe age 20 GPA 4. 0 gender m class 2 first. Name Mary last. Name Doe age 21 GPA 4. 0 gender f class 3 10 . . . Source: Pearson Education, Inc.