1. Structures
• A structure is a collection of related elements grouped together under a single name.
• Elements in a structure can be of same or different types.
• A smallest element in a structure is called a field.
• The difference between an array and a structure is that all elements in an array must be of
same type, while the elements in a structure can be of the same or different types.
Example: Structure
• C has two ways to declare a structure:
1) Tagged Structure
2) Type-defined Structures
Tagged Structure Example
• A tagged structure starts with a keyword struct.
• The second element in the declaration is the tag, an identifier for the structure.
• If the structure is concluded with a semicolon after the closing brace, no variables are
defined the structure is simply a type template with no associated storage.
Type Declaration with type-def
• A powerful way to declare a structure
• Format of typedef
• Type-defined structure differs from the tagged structure in two ways.
1. The keyword, typedef, is added to the beginning of the definition.
2. An identifier is required at the end of the block, the identifier is the type definition name
2. Declaring and Initialization
• Rules for structure initialization are similar to the rules for array intialization
• The initializers are enclosed in braces and separated by commas.
• They must match their corresponding types in the structure definition.
• When one or more intializers are missing, the structure elements will be assigned null
values, zero for integers and floating-point numbers, and null(‘0’) for characters and
strings.
Example: Initializing structures
Referencing Individual Fields
• Structure field can be accessed similar to an individual variable
• A field in a structure can be referred by referring to both the structure and the field.
Syntax
• To refer to a field in a structure, use the structure variable-identifier and then the field
identifier. The structure variable-identifier is separated from the field identifier by a dot.
Example: Structure Direct Selection operator
Defining Arrays for Structure
• Structures can have one or more arrays as members
• Arrays can be accessed either through indexing or through pointers
3. • They are properly qualified with direct selection operator
Example: Arrays in structures
• The student name and midterm stores are declared as arrays
• Initialization student s={“john”,{92,80,70},87};
Array of structures
• An array of structures needs to be created in many situations.
• Examples: Array of structure
• Use of an array of students to work with a group of students stored in a structure.
• Declare group of students as array
• Declare each student details as structure
• A structure declaration using arrays:
struct student
{
char name[15];
introllno;
char gender;
};
student stuAry[50];
Initialization
• An array of structures can be initialized in the same way as arrays in C
Example: Initializing an array of structures:
struct student
{
char name[15];
introllno;
char gender;
};
student stuAry[3]={ { “ram”,1234 , ‘m’}
{ “ravi”,2345 , ‘m’}
{ “sasi”,3456, ‘f’ }
};
• The above initialized values will be assigned by compiler as
stuAry[0].name=ram stuary[0].rollno=1234 stuary[0].gender=m
stuAry[1].name=ravi stuary[1].rollno=2345 stuary[1].gender=m
stuAry[2].name=sasi stuary[2].rollno=3456 stuary[2].gender=f
Unions
• The Union is a construct that allows memory to be shared by different types of data.
4. • A union is like a struct, but only one of its members is stored, not all
• I.e., a single variable may hold different types at different times
• Storage is enough to hold largest member
• Members are overlaid on top of each other
• Syntax: union union_name { <data-type> element 1;
<data-type> element 2;
<data-type> element 3; }union_variable;
• It is programmer’s responsibility to keep track of which type is stored in a union at any
given time!
• Example
structtaggedItem {
enum {iType, fType, cType} tag;
union {
intival;
float fval;
char *sval;
} u;
};
Unions are used much less frequently than structs — mostly
• in the inner details of operating system
• in device drivers
• in embedded systems where you have to access registers defined by the
hardware
Storage
• Size of union is the size of its largest member
• Avoid unions with widely varying member sizes
• for the larger data types, consider using pointers instead
Referencing Unions
• Rules for referencing a Union are identical to those for structure
• Direct Selection (dot) operator is used to reference individual fields within the
union
5. • When a union is referenced through pointer, the selection operator (arrow) can be
used
Example
ShareData.num
ShareData.chAry[0]
Initialization
• Union may only be initialized to a value appropriate for the type of its first
member
• The other types can only be initialized by assigning values or reading values into
the union.
• Enclose the values in a set of broces, even if there is only one value, when
initializing the union