Structure, Union, and Enumerated Data Types
Introduction
Allows the programmer to create custom data types in five different ways:
• The structure, which is a collection of variables under one name.
• Types created with typedef, which defines a new name for an existing type.
• The union, which enables the same piece of memory to be defined as two or more
types of variables.
• The enumeration, which is a list of symbols.
• The bit-field, a variation of the structure, which allows easy access to the bits of a word.
Structure
Structure is a user-defined data type in C that allows combining data of different types
together.
Example use case: Storing student information (name, age, branch, etc.).
Syntax:
struct [structure_tag] {
// member variable1
// member variable2
...
} [structure_variables];
Example:
struct Student {
char name[25];
int age;
char branch[10];
char gender; // F for female and M for male
};
Declaring Structure Variables
Declared either with structure or after defining structure.
Separately:
struct Student {
char name[25];
int age;
char branch[10];
char gender;
};
struct Student S1, S2;
With structure definition:
struct Student {
char name[25];
int age;
char branch[10];
char gender;
} S1, S2;
Accessing Structure Members
Use dot (.) operator:
s1.age = 18;
strcpy(s1.name, "Viraaj");
Using scanf:
scanf("%s", s1.name);
scanf("%d", &s1.age);
Structure Initialization
struct Patient {
float height;
int weight;
int age;
char name[10];
};
Initialization:
struct Patient p1 = {180.75, 73, 23, "Munwar"};
OR
p1.height = 180.75;
p1.weight = 73;
Array of Structure
Declare array of structures:
struct Employee emp[5];
Each element is a structure variable.
Example:
struct Employee {
char ename[10];
int sal;
};
Loop and access with emp[i].ename, emp[i].sal
Nested Structures
Structures can contain other structures.
Example:
struct Student {
char name[30];
int age;
struct Address {
char locality[50];
char city[50];
int pincode;
} addr;
};
Structure as Function Arguments
Structures can be passed as arguments to functions.
Example:
void show(struct Student st);
show(std);
Unions
Like structures, but all members share the same memory space.
Syntax:
union item {
int m;
float x;
char c;
} it1;
Only one member can be accessed at a time.
Accessing Union Members
Similar to structure member access using dot operator.
it.a = 12;
it.b = 20.2;
it.c = 'z';
Array of Union Variables
Union can be declared in arrays:
union category {
int intClass;
char chrDeptId[10];
};
union category catg[10];
catg[0].intClass = 10;
catg[5].chrDeptId = "DEPT_001";
Unions inside Structures
A union can be embedded inside a structure.
Example:
struct student {
union {
char name[10];
int roll;
};
int mark;
};
Structures inside Unions
A structure can be embedded inside a union.
Example:
union details {
struct student {
char name[30];
int rollno;
float percentage;
} s1;
};
Enumerated Data Types
Used to assign names to integral constants.
Syntax:
enum enum_name { const1, const2, ... };
Example:
enum week { Mon=10, Tue, Wed, Thur, Fri=10, Sat=16, Sun };
Structure vs Union
| Feature | Structure | Union |
|-----------------------|----------------------------------------|-------------------------------------------|
| Keyword | struct | union |
| Memory Allocation | Each member gets separate memory | All members share
same memory |
| Value Change Impact | Does not affect others | Affects all members
|
| Initialization | All members can be initialized | Only first member initialized
|
| Size | Sum of all members' sizes | Size of largest member
|
| Usage | Store multiple data types | Store one of many data types
|
| Access | Any member at any time | One member at a time
|
| Array Support | Supports flexible array | Does not support flexible array
|
Advantages of Structures
• Gather multiple data elements together.
• Useful for representing records.
• Easy maintenance.
• Structures can be passed as function arguments.
• Arrays of structures can store multiple records.
Advantages of Unions
• Less memory usage.
• Suitable when only one variable is used at a time.
• Efficient memory utilization.
Disadvantages of Structures
• Complex and harder to manage for large projects.
• Code maintenance becomes difficult.
• Slower due to individual memory allocations.
Disadvantages of Unions
• Only one member accessible at a time.
• Cannot use all members with different values simultaneously.
• One memory location shared.