180 likes | 372 Views
User-defined Structure Types. Structures: collection of data of different types. We can define a structure type student_t : typedef struct { string name; int age; char sex; double grade_avg; } student_t; typedef statement itself allocates no memory.
E N D
User-defined Structure Types • Structures: collection of data of different types. • We can define a structure type student_t : • typedef struct • { • string name; • int age; • char sex; • double grade_avg; • } student_t; • typedef statement itself allocates no memory. • A variable declaration is required to allocate storage space for a structured data object. E.g. student_t one_student; This type definition is a template that describes the format of a student structure and the name and type of each component.
How to reference a member of a Structure • We can reference a component of a structure by using the direct component selection operator, which is a period. • one_student . name • one_student . age • one_student . sex • one_student . grade_avg • A name chosen for a component of one structure may be the same as the name of a component of another structure.
.name D a v i d \0 ? ? ? ? .age 22 .sex M 90.0 .grade_avg Assigning Values to Components of variable one_student • one_student . Name= “David”; • one_student . age = 22; • one_student . sex = ‘M’; • one_student . grade_avg = 90.0; • variable one_student, a structure of type student_t
Hierarchical Structure • Hierarchical structure => a structure containing components that are structures (arrays or structs). • typedef struct • { • char name[10]; • int age; • char sex; • double grade_avg; • } student_t; • Note: To avoid confusion, we choose user_defined type names that use lowercase letters and end in the suffix_t.
F i j i \0 ? ? ?……... 178 0 E 17 50 S variable resort Hierarchical Structure .place typedef struct { int degrees, minutes; char direction; } long_lat_t; .longitude .latitude typedef struct { char place[20]; long_lat_t longitude, latitude; }location_t ; location_t resort; ReferenceData Type Value resort . latitude long_lat_t 17 50 ‘S’ resort . place ?? resort . longitude . direction ? ?
Manipulating Whole Structure • The name of a structure type variable used with no component selection operator refers to the entire structure. • A new copy of a structure’s value can be made by simply assigning one structure to another. • Example: • student_t student_one, • student_two; • student_one = student_two;
Arithmetic Operations and Comparisons • Aggregate arithmetic operations and comparisons are not allowed. • Examples: student_t stu1,stu2; stu1 = stu1 * stu2; // Not allowed if (stu1 < stu2) // Not allowed
Aggregate I/O is not permitted • cin >> stu1; // Not allowed • We must input or output a struct variable one member at a time. • Examples: • cin >> stu1 . name; • cin >> stu1 . Age;
Structure Type Data as Input Parameter Input parameter • void print_student(student_t stu) • { • cout << “Name : “<< stu . name); • cout << “Age: “ << stu . age); • cout << “Sex: “<< stu . sex); • cout << “Grade Avg: “<< stu . grade_avg; • } • Call statement: print_student( one_student );
Function Comparing Two Structured Values for Equality • int student_equal (student_t stu_1, // takes two students • student_t stu_2) // as input argument • { • return ( stu_1 . name = = stu_2 . name && • stu_1 . age == stu_2 . age && • stu_1 . sex == stu_2 . sex && • stu_1 . grade_avg == stu_2 . grade_avg); • } • // returns 1 if all components match, otherwise returns 0.
Function with a Structured Output Argument • void student_info (student_t *stup) // address of student_t • { • cout << "Enter name"<< endl; • cin >> (*stup) . name; • cout << "Enter age"<< endl; • cin >> (*stup). age; • cout << (*stup) . name << endl; • cout << (*stup) . age << endl; • }
Indirect Component Selection Operator • int scan_student (student_t *stup) // address of student_t • { • cout << "Enter name"<< endl; • cin >> stup -> name; • cout << "Enter age"<< endl; • cin >> stup -> age; • cout << stup -> name << endl; • cout << stup -> age << endl; • }
Function that Returns a Structured Result Type • student_t get_student (void) • { • student_t stu; • cin>> stu . name; • cin >> stu . age; • cin >> stu . sex; • cin >> stu . grade_avg; • return (stu); • }
893245521 593245422 693245521 493245926 393246525 883245521 3.98 2.71 3.41 3.91 2.71 3.71 Array of Structures stulist[0] . id Array stulist .id .gpa • const int MAX_STU = 50; • typedef struct • { • int id; • double gpa; • } student_t; student-_t stulist[MAX_STU]; stulist[0] stulist[1] stulist[2] stulist[3] stulist[4] stulist[5]
Union Types • typedef union • { • int wears_wig; // if the person is bald, we will // notice if he wears a wig • char color[20]; // if the person has hair, we will // record hair color • } hair_t; • hair_t hair_data; // creates a variable built on the // template of the type definition • hair_data does not contain both wears_wig and color. • It has either wear_wig or color. • The amount of memory is determined by the largest component of the union.
Union Type • typedef struct • { • int bald; // component that indicates which // interpretation of union is correct at present • hair_t h; • } hair_info_t; • If the person is bald, the wear_wig interpretation of component h is valid. • For nonbald person, the color interpretation is valid and represents the color of the person’s hair.
Function that Display a Structure with Union Type Component • void print_hair_info(hair_info_t hair) • { • if (hair.bald) • { • cout << “Subject is bald”; • if (hair.h.wears_wig) • cout << “, but wears a wig.” <<endl; • else • cout<<“and does not wear a wig.”<<endl; • } • else • cout<<“Subject’s hair color is : ”<< hair.h.color<<endl; • }
Union • A union is a derived data type- like a structure- whose members share the same storage space. • For different situations in a program, some variables may not be relevant, but other variables are relevant. • So a union shares the space instead of wasting storage on variables that are not being used. • The members of a union can be of any type. • The number of bytes used to store a union must be at least enough to hold the largest member. • In most cases, unions contain two or more data types. • Only one member, and thus one data type , can be referenced at a time. • It is the programmer’s responsibility to ensure that the data in a union is referenced with the proper data type.