1.10 Structures, Unions and User Defined Data Types in C Programming
Module 1.10 • Heterogeneous Records, Shared Memory Unions & Custom Type Aliases
1.10.1 Introduction
In real-world applications, a single object often contains multiple pieces of related information.
For example, a book may have:
- Title
- Author
- Price
- Number of Pages
Since these values are different data types, a normal array cannot store them together efficiently. C provides Structures and Unions to solve this problem.
1.10.2 What is a Structure?
A Structure is a user-defined data type that groups related variables of different data types under a single name.
Real-Life Example
Book Record
Title : C Programming
Pages : 650
Price : 799.50
A structure allows all these details to be stored together.
1.10.3 Defining a Structure
Syntax
struct structure_name
{
datatype member1;
datatype member2;
datatype member3;
};
Example
struct Book
{
char title[40];
int pages;
float price;
};
Here:
title→ stringpages→ integerprice→ floating-point value
1.10.4 Creating Structure Variables
After defining a structure, variables can be created.
Example
struct Book b1;
struct Book b2;
Each variable contains all structure members.
1.10.5 Structure Initialization
Values can be assigned during declaration.
Example
struct Book b1 =
{
"Data Structures",
520,
699.50
};
1.10.6 Accessing Structure Members
The dot operator (.) is used.
Example Program
#include <stdio.h>
struct Book
{
char title[40];
int pages;
float price;
};
int main()
{
struct Book b1 =
{
"Data Structures",
520,
699.50
};
printf("Title : %s\n", b1.title);
printf("Pages : %d\n", b1.pages);
printf("Price : %.2f\n", b1.price);
return 0;
}
Output
Title : Data Structures
Pages : 520
Price : 699.50
1.10.7 Assigning Values to Members
Example
struct Book b1;
b1.pages = 450;
b1.price = 599.00;
String members require strcpy().
strcpy(b1.title,"Algorithms");
1.10.8 Structure Assignment
Structures of the same type can be copied directly.
Example
struct Book b1 =
{
"Networking",
400,
550.00
};
struct Book b2;
b2 = b1;
Now all member values are copied.
1.10.9 Array of Structures
Many records can be stored using arrays.
Example Program
#include <stdio.h>
struct Product
{
int id;
float price;
};
int main()
{
struct Product items[3] =
{
{101,120.50},
{102,245.75},
{103,399.99}
};
int i;
for(i=0;i<3;i++)
{
printf("ID=%d Price=%.2f\n",
items[i].id,
items[i].price);
}
return 0;
}
Output
ID=101 Price=120.50
ID=102 Price=245.75
ID=103 Price=399.99
1.10.10 Arrays Inside Structures
Structures can contain arrays.
Example
struct Employee
{
char name[30];
int salary[12];
};
salary stores monthly salaries for one year.
1.10.11 Arrays Within Structures Example
#include <stdio.h>
struct Employee
{
char name[20];
int salary[3];
};
int main()
{
struct Employee emp =
{
"Rahul",
{25000,26000,27000}
};
int i;
for(i=0;i<3;i++)
{
printf("%d ",emp.salary[i]);
}
return 0;
}
Output
25000 26000 27000
1.10.12 Nested Structures
A structure can contain another structure.
Example
struct Address
{
char city[20];
int pincode;
};
struct Customer
{
char name[30];
struct Address addr;
};
1.10.13 Nested Structure Example
#include <stdio.h>
struct Address
{
char city[20];
int pincode;
};
struct Customer
{
char name[20];
struct Address addr;
};
int main()
{
struct Customer c1 =
{
"Vikram",
{"Chennai",600001}
};
printf("%s\n",c1.name);
printf("%s\n",c1.addr.city);
printf("%d\n",c1.addr.pincode);
return 0;
}
Output
Vikram
Chennai
600001
1.10.14 Structure Pointers
A pointer can point to a structure.
Declaration
struct Customer *ptr;
Example
struct Customer c1;
ptr = &c1;
1.10.15 Accessing Members Using Pointer
Two methods exist.
Method 1
(*ptr).name
Method 2
ptr->name
Arrow operator is preferred.
1.10.16 Structure Pointer Example
#include <stdio.h>
struct Vehicle
{
char model[20];
int year;
};
int main()
{
struct Vehicle v1 =
{
"SUV",
2025
};
struct Vehicle *ptr;
ptr = &v1;
printf("%s\n",ptr->model);
printf("%d\n",ptr->year);
return 0;
}
Output
SUV
2025
1.10.17 Passing Structure to Functions
Structures can be passed like ordinary variables.
Example
void display(struct Vehicle v)
{
printf("%s",v.model);
}
1.10.18 Passing Structure by Value
#include <stdio.h>
struct Vehicle
{
char model[20];
int year;
};
void show(struct Vehicle v)
{
printf("%s %d\n",
v.model,
v.year);
}
int main()
{
struct Vehicle v =
{
"Sedan",
2024
};
show(v);
return 0;
}
1.10.19 Passing Structure by Reference
More efficient for large structures.
Example
void update(struct Vehicle *v)
{
v->year = 2026;
}
Changes affect original data.
1.10.20 Returning Structures from Functions
Example
struct Result
{
int total;
};
struct Result calculate()
{
struct Result r;
r.total = 450;
return r;
}
1.10.21 Self-Referential Structures
A structure containing a pointer to itself is called a self-referential structure.
Example
struct Node
{
int data;
struct Node *next;
};
This concept is used in linked lists.
1.10.22 Introduction to Linked Lists
A linked list consists of nodes. Each node contains:
- Data Part
- Address Part
Example:
[12|*] -> [25|*] -> [40|NULL]
Benefits:
- Dynamic size
- Easy insertion
- Easy deletion
1.10.23 Linked List Node Creation
struct Node
{
int data;
struct Node *next;
};
1.10.24 Traversing a Linked List
while(ptr != NULL)
{
printf("%d ",ptr->data);
ptr = ptr->next;
}
1.10.25 Dynamic Node Allocation
struct Node *newNode;
newNode = (struct Node*) malloc(sizeof(struct Node));
1.10.26 What is a Union?
A Union is a user-defined type where all members share the same memory location. Unlike structures:
- Structure → Separate memory
- Union → Shared memory
1.10.27 Defining a Union
Syntax
union Data
{
int id;
float salary;
char grade;
};
1.10.28 Union Example
#include <stdio.h>
union Data
{
int id;
float salary;
char grade;
};
int main()
{
union Data d;
d.id = 250;
printf("%d\n",d.id);
d.grade = 'A';
printf("%c\n",d.grade);
return 0;
}
Output
250
A
1.10.29 Understanding Union Memory
Structure:
id → separate memory
salary → separate memory
grade → separate memory
Union:
id
salary
grade
↓
Same Memory Location
Only one value should be used at a time.
1.10.30 Size of Structure vs Union
#include <stdio.h>
struct Sample
{
char ch;
int num;
float value;
};
union Test
{
char ch;
int num;
float value;
};
int main()
{
printf("%lu\n", sizeof(struct Sample));
printf("%lu\n", sizeof(union Test));
return 0;
}
The union size equals the size of the largest member.
1.10.31 Structure vs Union
| Feature | Structure | Union |
|---|---|---|
| Memory | Separate | Shared |
| Multiple values | Yes | No |
| Storage | More | Less |
| Access | All members | One member at a time |
| Usage | Records | Memory optimization |
1.10.32 Union Inside Structure
struct Student
{
char name[20];
union
{
int marks;
char grade;
} result;
};
1.10.33 typedef Keyword
typedef creates an alias name for a data type.
Example
typedef int Marks;
Now:
Marks physics;
Marks chemistry;
Equivalent to:
int physics;
int chemistry;
1.10.34 typedef with Structures
Without typedef:
struct Employee e1;
Using typedef:
typedef struct
{
int id;
float salary;
} Employee;
Employee e1;
Cleaner and easier to read.
1.10.35 typedef Example
#include <stdio.h>
typedef struct
{
int id;
float salary;
} Employee;
int main()
{
Employee e1 =
{
501,
42000.50
};
printf("%d\n",e1.id);
printf("%.2f\n",e1.salary);
return 0;
}
Output
501
42000.50
Applications of Structures
Structures are used in:
- Student Management Systems
- Banking Software
- Employee Databases
- Hospital Management Systems
- Inventory Applications
- File Handling
- Operating Systems
Applications of Unions
Unions are used in:
- Embedded Systems
- Device Drivers
- Communication Protocols
- Memory-Constrained Systems
- Hardware Register Access
Summary
- Structures group related data of different types.
- Dot operator accesses structure members.
- Arrow operator accesses members through pointers.
- Arrays and nested structures provide advanced data organization.
- Self-referential structures form the foundation of linked lists.
- Unions share memory among members and save space.
- typedef creates readable aliases for existing data types.
- Structures and unions are fundamental building blocks for advanced C programming.
Click your choice for each question to view feedback immediately. Complete all questions to evaluate your metric score.