🏗️ Structure in C Language
🔹 What is a Structure in C?A structure in C is a user-defined data type that allows grouping of different data types under a single name.
👉 Structures are used to represent real-world entities like students, employees, books, etc.
✅ Why Use Structures?
- To store related data of different types
- Improves program readability
- Helps in complex data handling
- Used in files, databases, and large programs
📌 Syntax of Structure
struct structure_name {
data_type member1;
data_type member2;
...
};
📌 Declaring Structure Variables
struct Student {
int roll;
char name[20];
float marks;
};
struct Student s1;
📌 Accessing Structure Members
Use dot (.) operator.
s1.roll = 1;
s1.marks = 85.5;
📘 Example Program: Structure in C
#include <stdio.h>
struct Student {
int roll;
char name[20];
float marks;
};
int main() {
struct Student s1 = {1, "Rahul", 90.5};
printf("Roll: %d\n", s1.roll);
printf("Name: %s\n", s1.name);
printf("Marks: %.2f", s1.marks);
return 0;
}
📂 Types of Structures in C
1️⃣ Simple Structure
A structure with basic data members.
struct Book {
int id;
char title[30];
};
2️⃣ Array of Structures
Used to store multiple records of the same structure.
#include <stdio.h>
struct Student {
int roll;
char name[20];
};
int main() {
struct Student s[2] = {
{1, "Amit"},
{2, "Neha"}
};
for(int i = 0; i < 2; i++) {
printf("%d %s\n", s[i].roll, s[i].name);
}
return 0;
}
3️⃣ Structure Within Structure (Nested Structure)
#include <stdio.h>
struct Date {
int day, month, year;
};
struct Student {
int roll;
char name[20];
struct Date dob;
};
int main() {
struct Student s1 = {1, "Anil", {10, 5, 2002}};
printf("%d %s %d/%d/%d",
s1.roll, s1.name,
s1.dob.day, s1.dob.month, s1.dob.year);
return 0;
}
4️⃣ Pointer to Structure
Used for efficient memory access.
#include <stdio.h>
struct Student {
int roll;
char name[20];
};
int main() {
struct Student s1 = {1, "Ravi"};
struct Student *ptr = &s1;
printf("%d %s", ptr->roll, ptr->name);
return 0;
}
👉 -> operator is used to access members using pointer.
5️⃣ Structure as Function Argument
void display(struct Student s) {
printf("%d %s", s.roll, s.name);
}
6️⃣ Structure as Function Return Type
struct Student getData() {
struct Student s = {1, "Kiran"};
return s;
}
7️⃣ Typedef with Structure
typedef struct {
int id;
char name[20];
} Employee;
int main() {
Employee e1 = {101, "Suman"};
printf("%d %s", e1.id, e1.name);
return 0;
}
⚖️ Difference Between Structure and Union
| Structure | Union |
|---|---|
| Separate memory for members | Shared memory |
| More memory usage | Less memory |
| All members accessible | One member at a time |
📚 FAQs on Structure in C
Q1. What is a structure?
A structure is a user-defined data type that groups different data types.
Q2. Why use structures instead of arrays?
Arrays store same data type, structures store different data types.
Q3. Can structures be nested?
Yes, one structure can be inside another structure.
Q4. How to access structure members using pointer?
Using the -> operator.
Q5. Does structure support functions?
Yes, structures can be passed to and returned from functions.
🔑 Key Points to Remember
- Structures store different data types
- Use dot (.) operator for normal access
- Use arrow (->) operator for pointers
- Widely used in real-world programs