Structure
Definition
Structures are user-defined
data types in the C language that allow us to group different types of
data (such as int, float, char, etc.) under a single name.
Each individual data element inside a structure is called a member.
OR
A structure is a user-defined data type in which different types of variables are grouped together under one name.
Declaring a Structure
Syntax:
struct structure_name { data_type variable1; data_type variable2; data_type variable3;};
Example:
struct Student { int roll; char name[20]; float marks;};
Memory Allocation
A structure is a user-defined data type in
which different data types are grouped together.
The memory of a structure is equal to the sum
of the memory of all its members.
Declaring a Structure Variable
After defining a structure, its variable can
be created in several ways.
Method
1: Variable outside the structure
struct Student s1;
Method
2: Variable declared along with structure
struct Student { int roll; char name[20]; float marks;} s1, s2;
Initialization of Structure
We can assign values to a structure variable
during initialization.
Example:
struct Student s1 = {101, "Amit", 89.5};
Accessing Structure Members
To access member values, the dot operator ( . ) is used.
Example:
printf("%d", s1.roll); // Prints roll valueprintf("%s", s1.name); // Prints nameprintf("%f", s1.marks); // Prints marks
Advantages of Structure
·
Stores different types of data together.
·
Helps manage data in a logical and organized
way.
·
Useful in large programs for real-world data
(students, employees, products).
·
Makes code readable and structured.
· Can be easily passed to functions.
Disadvantages of Structure
·
Uses more memory because each member has
separate memory.
·
Cannot contain functions/methods (C limitation).
·
Data security is low.
· Multiple structure variables consume large memory.
Types of Structure
1.
Simple Structure
2.
Array of Structures
3.
Nested Structure
4.
Structure with Functions
5. Pointer to Structure
1. Simple Structure
#include <stdio.h> struct Student { int roll; float marks;}; int main() { struct Student s; s.roll = 10; s.marks = 89.5; printf("Roll: %d\n", s.roll); printf("Marks: %.2f", s.marks); return 0;}
2. Array of Structures
#include <stdio.h> struct Student { int roll; float marks;}; int main() { struct Student s[3]; // Array of 3 students for(int i = 0; i < 3; i++) { printf("Enter roll and marks: "); scanf("%d %f", &s[i].roll, &s[i].marks); } printf("\n--- Student Data ---\n"); for(int i = 0; i < 3; i++) { printf("Roll: %d Marks: %.2f\n", s[i].roll, s[i].marks); } return 0;}
3. Nested Structure
#include <stdio.h> struct Address { char city[20]; int pin;}; struct Student { char name[20]; struct Address addr; // Nested structure}; int main() { struct Student s = {"Amit", {"Lucknow", 226001}}; printf("Name: %s\n", s.name); printf("City: %s\n", s.addr.city); printf("PIN: %d", s.addr.pin); return 0;}
4. Structure with Functions
#include <stdio.h> struct Student { int roll; float marks;}; void display(struct Student s) { printf("Roll: %d Marks: %.2f\n", s.roll, s.marks);} int main() { struct Student s1 = {15, 92.3}; display(s1); return 0;}
5. Pointer to Structure
#include <stdio.h> struct Student { int roll; float marks;}; int main() { struct Student s = {20, 88.6}; struct Student *ptr; ptr = &s; printf("Roll: %d\n", ptr->roll); printf("Marks: %.2f", ptr->marks); return 0;}
Union
Definition
A union is a user-defined data type in which
multiple variables are grouped together, but all members share the same memory location.
Therefore, only one member’s value
remains valid at a time.
Declaring a Union
Syntax:
union union_name { data_type member1; data_type member2; data_type member3;};
Example:
union Data { int i; float f; char ch;};
Declaring a Union Variable
Method 1: Outside the union
union Data d1;
Method
2: Along with the union
union Data { int i; float f; char ch;} d1, d2;
Initialization of Union
union Data d = {10}; // OR assign laterd.f = 22.5;
Note:
Only the last assigned value
remains valid.
Example Program
#include <stdio.h> union Data { int i; float f; char ch;}; int main() { union Data d; d.i = 10; printf("Integer: %d\n", d.i); d.f = 20.5; printf("Float: %.2f\n", d.f); d.ch = 'A'; printf("Character: %c\n", d.ch); return 0;}
Types of Union in C
1. Simple
Union
2.
Array of Union
3.
Union inside Structure
4.
Structure inside Union
1.
Simple Union
union Data { int i; float f;};
2.
Array of Union
union Data { int i; float f;}; union Data d[5];
3.
Union inside Structure
struct Student { char name[20]; union Marks { int theory; int practical; } m;};
4.
Structure inside Union
union Info { int id; struct Address { char city[20]; int pin; } addr;};
Advantages of Union
·
Uses very little memory because all members
share the same memory.
·
Useful when only one value is needed at a time.
·
Good for embedded systems and low-memory
devices.
· Helps create memory-efficient programs.
Disadvantages of Union
·
Only one member’s data is valid at a time.
·
Assigning value to one member erases the
previous value.
·
Debugging becomes difficult.
· Higher chances of errors in complex programs.
Difference Between Structure
and Union
|
Structure |
Union |
|
All members have separate memory. |
All members share the same memory. |
|
All values can be stored at the same
time. |
Only one value is valid at a time. |
|
More memory usage. |
Very low memory usage. |
|
Data does not overwrite. |
Data can overwrite. |
|
Size = sum of sizes of all members. |
Size = size of the largest member. |
No comments:
Post a Comment