Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structure (i.e. struct) An structure creates a user defined data type

Similar presentations


Presentation on theme: "Structure (i.e. struct) An structure creates a user defined data type"— Presentation transcript:

1 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

2 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 struct stype { type1 member1; type2 member2; . . . typeN memberN; };

3 Structure declaration (example)
struct student { string firstName; string lastName; int age; float GPA; char gender; int class; }; 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

4 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 Example: student S1; // variable declaration! S1.firstName= “John”; S1.lastName= “Doe”; S1.age= 20; S1.gender= ‘m’; // happy birthday! S1.age+= 1;

5 structs as Operands and Arguments
struct copy or assignment student S2= S1; // copy S1 to S2 Copies each member from S1 struct to S2 struct

6 Storage Structure member elements are stored in sequential memory locations Student S1 Memory Address Member Value 0x1xxx firstName* John 0x1yyy lastName* Doe 0x1100 age 20 0x1104 GPA 4.0 0x1108 gender** m 0x1109 class 2 * size of a string ** may actually use more than one byte

7 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

8 Passing struct by Reference
Same as any other reference parameter use & to identify in parameter list void printStudent(student &s) struct members can be modified Can also use constant reference precede parameter with reserved word const E.g. void printStudent(const student &s) const prevents the function from changing any member in the structure

9 Function printStudent()
void printStudent(const student &s) { cout << “Student Information: " << endl; cout << " Name : " << s.firstName << “ “ << s.lastName << endl; cout << " Age : " << s.age << endl; cout << " Gender : " << s.gender << endl; cout << " Class : " << s.class << endl; cout << " GPA : " << s.GPA << endl; } 0x1104

10 Array of Structs . Declaration of an array of structs
firstName John lastName Doe age 20 GPA 4.0 gender m class 2 Declaration of an array of structs student S[10]; Storage for 10 student structs Can pass to a function printStudent(S[i]); Storage is in sequential memory locations S[0] firstName Mary lastName Doe age 21 GPA 4.0 gender f class 3 S[1] .


Download ppt "Structure (i.e. struct) An structure creates a user defined data type"

Similar presentations


Ads by Google