Presentation is loading. Please wait.

Presentation is loading. Please wait.

User-defined Structure Types Structures: collection of data of different types. We can define a structure type student_t : typedef struct { string name;

Similar presentations


Presentation on theme: "User-defined Structure Types Structures: collection of data of different types. We can define a structure type student_t : typedef struct { string name;"— Presentation transcript:

1 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.

2 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.

3 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 D a v i d \0 ? ? ? ? 22 M 90.0.name.age.sex.grade_avg

4 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.

5 Hierarchical Structure typedef struct { char place[20]; long_lat_t longitude, latitude; }location_t ; location_t resort; typedef struct { int degrees, minutes; char direction; } long_lat_t; F i j i \0 ? ? ?……... 178 S50 0 17 E variable resort Reference Data Type Value resort. latitude long_lat_t 17 50 ‘S’ resort. place ? ? resort. longitude. direction ? ?.place.longitude.latitude

6 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;

7 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

8 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;

9 Structure Type Data as 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 ); Input parameter

10 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.

11 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; }

12 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 name << endl; cout age << endl; }

13 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); }

14 Array of Structures const int MAX_STU = 50; typedef struct { int id; double gpa; } student_t; student-_t stulist[MAX_STU]; 8932455213.715932454222.716932455213.984932459263.413932465252.718832455213.91 Array stulist stulist[0] stulist[1] stulist[2] stulist[3] stulist[4] stulist[5] stulist[0]. id.id.gpa

15 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.

16 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.

17 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; }

18 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.


Download ppt "User-defined Structure Types Structures: collection of data of different types. We can define a structure type student_t : typedef struct { string name;"

Similar presentations


Ads by Google