Chapter 11 Structure and Union Types Instructor: Alkar / Demirer
Dec 20, 2015
Chapter 11Structure and Union Types
Instructor:Alkar / Demirer
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-2
User-Defined Structure Types
• A database is a collection of information subdivided into recordsrecords.– A recordrecord is a collection of information of one data
object (e.g., ID, name, and age of a student).
• C allows us to define a new data type (called structure typestructure type) for each category of a structured data object.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-3
Declaring Structure Types (1/2)• Syntax of the structure type:typedef structtypedef struct{
type1 id1;type2 id2;…
} struct_typestruct_type;• E.g.,typedef structtypedef struct{
char[20] name;int age;
} student_info;student_info;
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-4
Declaring Structure Types (2/2)
• Declaration:student_infostudent_info student1,
student2 = {“Wang”, 18};• A hierarchical structure is a structure containing
components which are also structures.typedef structtypedef struct{
int NumOfStudents;student_infostudent_info students[20];
} class_info;class_info;
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-5
Manipulating Structure Types (1/2)
• We can reference a component of a structure by the direct component selection operatordirect component selection operator, which is a period.
• E.g., strcpy(student1.name, “Chao”);student1.age = 18;printf(“%s is in age %d\n”, student1.name, student1.age);
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-6
Manipulating Structure Types (2/2)
• The direct component selection operator direct component selection operator has the highest priority in the operator precedence.– student1.age+student2.age+…;– The value of student1.age is referenced first.
• The copy of an entire structure can be easily done by the assignment operator.– student1 = student2;– Each component in one structure is copied into the
corresponding component in the other structure.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-7
Function with a Structured Input Parameter (1/2)• Suppose there is a structure defined as follows.• typedef struct{
char name[20];double diameter;int moons;double orbit_time,
rotation_time;} planet_tplanet_t;
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-8
Function with a Structured Input Parameter (2/2)• When a structure variable is passed as an input
argument to a function, all its component values are copied into the local structure variable.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-9
Function with a Structured Input/Output Argument• For the following function, we have to call it by
“scan_planet(&¤t_planet);”– The input argument is also used to store the result.
“*plnp” is parenthesized because & operator has higher precedence.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-10
Data Areas of call to scan_planet (&¤t_planet);
plnp is a pointer which points to the same data area of current_planet.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-11
Step-by-Step Analysis of the Indirect Reference
Reference Type Value
plnp planet_t * Address of structure refers to current_planet
*plnp planet_t Real structure of current_planet
(*plnp).diameter double 12713.5
&(*plnp).diameter double * Address of diameter of current_planet structure
• ““&(*plnp).diameter&(*plnp).diameter” is evaluated as shown in the following table.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-12
Indirect Component Selection Operator• In the above example, we use direct component
selection operator: period.– e.g., &(*plnp)..diameter
• C also provides indirect component selection indirect component selection operatoroperator: ->->.– e.g., “&plnp-->>diameter” is the same as
“&(*plnp).diameter”.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-13
Function Returning a Structured Result Type (1/2)
• The structure variable can also be used as the return value of a function.
Use direct component selection operator.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-14
Function Returning a Structured Result Type (2/2)• Suppose the current time is 21:58:32, and the elapsed
time is 97 seconds.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-15
Arrays of Structures (1/2)
• We can also declare an array of structures.• E.g., typedef strucut{
int id;double gpa;
} student_t;• Usage:student_t stulist[50];stulist[3].id = 92922023;stulist[3].gpa = 3.0;
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-16
Arrays of Structures (2/2)• The array of structures can be simply manipulated as
arrays of simple data types.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-17
Union Types• UnionUnion is a data structure which represents only one of
the component in the declaration.• E.g., typedef unionunion{
int wears_wig;char color[20];
} hair_thair_t;• Suppose we declare a vairable “hait_t hair_data;”.
• This hair_data contains either the wears_wig component or the color component but not both.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-18
A Function Using a Union Structure• Suppose we have a structure variable.typedef struct{
int bald;hair_t h;
} hair_info_thair_info_t• We can use this structure to reference the correct component.
Use the wears_wig component.
Use the color component.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 11-19
Two Interpretations of the Union Variable hair_t• The memory content of hair_t depends on which component is
referenced.– The memory allocated for hair_t is determined by the largest component in
the union.
• Referencing the correct union component is the programmer’s responsibility.
The wears_wig component is referenced.
The color component is referenced.