Structures and unions allow developers to create their own custom data types in C. They are widely used in real-world applications such as databases, file handling, compilers, and system-level programming.
1. Introduction to Structures in C
A structure is a user-defined data type that groups variables of different data types under a single name.
For example, storing student data (name, roll number, marks) is easy using structures.
Why Structures?
- Combine different data types
- Easy to manage complex records
- Used in real-world systems (employee records, databases, etc.)
2. Defining a Structure
Syntax
struct structure_name {
data_type member1;
data_type member2;
...
};
Example: Student Structure
struct Student {
int roll;
char name[20];
float marks;
};
3. Declaring Structure Variables
Method 1: After structure definition
struct Student s1, s2;
Method 2: Along with definition
struct Student {
int roll;
char name[20];
float marks;
} s1, s2;
4. Accessing & Processing Structure Members
Use the dot operator (.).
Example
#include <stdio.h>
struct Student {
int roll;
char name[20];
float marks;
};
int main() {
struct Student s;
printf("Enter roll, name, marks: ");
scanf("%d %s %f", &s.roll, s.name, &s.marks);
printf("Roll: %d\nName: %s\nMarks: %.2f\n",
s.roll, s.name, s.marks);
return 0;
}
5. User-Defined Data Types (typedef)
typedef allows you to define an alias for a structure to simplify usage.
Without typedef
struct Student s1;
With typedef
typedef struct {
int roll;
char name[20];
} Student;
Student s1; // No need to write 'struct'
6. Structures and Pointers
Pointers can be used to access structure variables using the arrow operator (->).
Pointer to a Structure
struct Student {
int roll;
char name[20];
float marks;
};
struct Student s;
struct Student *ptr = &s;
Accessing Using Pointer
ptr->roll = 10;
ptr->marks = 88.5;
Equivalent to:
s.roll = 10;
7. Passing Structures to Functions
You can pass structures in three ways:
7.1 Pass by Value
Copies the entire structure.
void display(struct Student s);
Example
void display(struct Student s) {
printf("%d %s %.2f", s.roll, s.name, s.marks);
}
7.2 Pass by Reference (Efficient)
Using pointers:
void display(struct Student *s);
Example
void display(struct Student *s) {
printf("%d %s %.2f", s->roll, s->name, s->marks);
}
7.3 Returning Structure from Function
struct Student input() {
struct Student s;
scanf("%d %s %f", &s.roll, s.name, &s.marks);
return s;
}
8. Introduction to Unions in C
A union is similar to a structure but with one major difference:
๐ก Key Difference:
- Structure โ Allocates memory for all members.
- Union โ Allocates memory for the largest member only.
- All members share the same memory location.
Unions are memory-efficient and useful when only one member is used at a time.
9. Defining a Union
Syntax
union union_name {
data_type member1;
data_type member2;
};
Example
union Data {
int i;
float f;
char str[20];
};
10. Working with Unions
#include <stdio.h>
union Data {
int i;
float f;
char str[20];
};
int main() {
union Data d;
d.i = 10;
printf("i = %d\n", d.i);
d.f = 22.5;
printf("f = %.2f\n", d.f);
// Now d.i value becomes corrupted
return 0;
}
Why?
All members share the same memory. Assigning f overwrites i.
11. Structure vs Union (Comparison)
| Feature | Structure | Union |
|---|---|---|
| Memory | Sum of all members | Size of the largest member |
| Multiple members at once | โ Yes | โ No |
| Usage | Records | Memory-saving scenarios |
| Data Integrity | High | Low (overwritten) |
Conclusion
Structures and unions are powerful tools in C for creating custom data types. Structures help organize different data types into one composite unit, while unions are useful when memory optimization is required.
๐ Citations
๐ View other articles about C Programming:
https://savanka.com/category/learn/c-programming/
๐ External C Documentation:
https://www.w3schools.com/c/