Unit 4
Unit 4
SOLVING AND C
PROGRAMMING
UNIT-4
STRUCTURES AND UNION
DEFINITION
• Used for handling a group of logically related data items
• Examples:
Student name, roll number, and marks
• Real part and complex part of a complex number
• Helps in organizing complex data in a more meaningful way
• The individual structure elements are called members
struct tag {
member 1;
member 2;
:
member m;
};
– struct is the required C keyword
– tag is the name of the structure
– member 1, member 2, … are individual member declarations
• The individual members can be ordinary variables,
pointers, arrays, or other structures (any data type)
– The member names within a particular structure must
be distinct from one another
– A member name can be the same as the name of a
variable defined outside of the structure
• Once a structure has been defined, the individual structure-
type variables can be declared as:
struct tag var_1, var_2, …, var_n;
ARRAY OF STRUCTURES
• Once a structure has been defined, we can
declare an array of structures
struct student class[50];