Structure
(स्ट्रक्चर)
Definition (परिभाषा)
Structures C भाषा में user-defined
data type होते
हैं, जिनकी मदद से हम अलग-अलग प्रकार के data (जैसे
int, float, char आदि) को एक ही नाम के अंतर्गत समूहित (group) कर
सकते हैं। Structure के अंदर मौजूद प्रत्येक अलग-अलग data को member कहा जाता है।
or
Structure एक user-defined data type है जिसमें अलग-अलग प्रकार के कई variables को एक ही नाम के अंतर्गत समूहित (group) किया जाता है।
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
Structure एक user-defined data type
है जिसमें अलग–अलग data types (int, float,
char आदि) को एक साथ group
किया जाता है।
Structure की memory allocation उसके सभी members की memory को जोड़कर की जाती है।
Structure
define करने के बाद
उसका variable कई तरीकों से बनाया जा सकता है।
Method 1: Structure के बाहर variable
struct Student s1;
Method 2: Structure के साथ ही variable
struct Student {
int roll;
char name[20];
float marks;
} s1, s2;
Initialization of Structure
Structure
variable को value से initialize ऐसे किया जाता है:
Example:
struct Student s1 = {101, "Amit", 89.5};
Accessing Structure Members
Structure के
किसी member की
value को access करने के लिए डॉट
ऑपरेटर ( . ) का उपयोग किया जाता है।
✔ Example:
printf("%d", s1.roll); // roll की value प्रिंट करेगाprintf("%s", s1.name); // name की value प्रिंट करेगाprintf("%f", s1.marks); // marks की value प्रिंट करेगा
Advantages of Structure
- अलग-अलग प्रकार के data (int, float, char) को एक साथ store कर सकते हैं।
- Data को logical और व्यवस्थित तरीके से manage किया जा सकता है।
- बड़े programs में real-world data (student,
employee, product) store करने में बहुत उपयोगी।
- Code को readable और structured बनाता है।
- Functions के साथ आसानी से pass किया जा सकता है।
Disadvantages of Structure
- Memory usage अधिक होती है, क्योंकि हर member अलग memory लेता है।
- Structure में functions या methods नहीं होते (C language limitation)।
- Data को सुरक्षित (secure) रखना थोड़ा कठिन होता है।
- Same structure type के कई variables memory में जगह लेते हैं।
Types of Structure
- Simple Structure
- Array of Structures
- Nested Structure
- Structure with Functions
- Pointer to Structure
1. Simple Structure
Simple
structure वह है जिसमें
केवल structure बनाकर उसका एक variable इस्तेमाल किया जाता है।
#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
Structure की array में एक ही structure type के कई records store किए जाते हैं।
#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
जब एक structure के अंदर दूसरा structure रखा जाए, उसे 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
Structure को function में pass किया जा सकता
है। (यह call by value या call by reference दोनों हो सकता है)
#include <stdio.h>
struct Student {
int roll;
float marks;
};
void display(struct Student s) { // Structure parameter
printf("Roll:
%d Marks: %.2f\n", s.roll,
s.marks);
}
int main() {
struct Student
s1 = {15, 92.3};
display(s1); // Passing
structure to function
return 0;
}
5. Structure Pointer
Structure variable को pointer से access करने पर -> operator का उपयोग किया
जाता है।
#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 (परिभाषा)
Union एक user-defined data type है जिसमें कई variables को एक साथ group किया जाता है, लेकिन सभी members एक ही memory location share करते हैं। इसलिए एक समय में केवल एक ही member का value सुरक्षित रहता है।
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
Union
variable दो तरीकों से
बनाया जा सकता है:
Method 1: Union के बाहर variable
union Data d1;
Method 2: Union के साथ ही variable
union Data {
int i;
float f;
char ch;
} d1, d2;
Initialization of Union
Union
variable को initialize ऐसे किया जाता है:
EX:- union Data d = {10};
Or assign later:
d.f = 22.5;
ध्यान रखें:
एक समय में
सिर्फ आखिरी assigned
value ही सही रहती
है।
#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 (साधारण
यूनियन)
यह सबसे basic union प्रकार है जिसमें केवल union declare करके उसके members उपयोग किए जाते हैं।
Example:
union Data {
int i;
float f;
};
2. Array of Union (Union की Array)
Union के कई variables को array के रूप में store किया जाता है।
Example:
union Data {
int i;
float f;
};
union Data d[5];
// array of 5 union variables
3. Union inside Structure
जब किसी structure के अंदर एक union declare किया जाए।
Example:
struct Student {
char name[20];
union Marks
{
int
theory;
int
practical;
} m;
};
4. Structure inside Union
जब union के अंदर एक structure declare किया जाए।
Example:
union Info {
int id;
struct Address
{
char
city[20];
int pin;
} addr;
};
Advantages of Union
- Memory बहुत कम उपयोग होती है, क्योंकि सभी members एक ही memory share करते हैं।
- जब एक समय में केवल एक data की जरूरत हो, तब useful।
- Embedded systems और low-memory applications में helpful।
- Memory-efficient programs बनाने में उपयोग।
Disadvantages of Union
- एक समय में सिर्फ एक ही member का डेटा सही रहता है।
- दूसरे member में value डालने से पुराना data erase हो जाता है।
- Debugging कठिन हो सकता है (कौन सा data valid है ध्यान रखना पड़ता है)।
- Complex programs में गलती होने की संभावना अधिक।
Difference Between Structure
and Union
|
Structure |
Union |
|
1. सभी members अलग-अलग memory लेते हैं। |
1.
सभी members एक ही memory share करते हैं। |
|
2. सभी values एक साथ store की जा सकती हैं। |
2.
एक समय में एक ही value store हो सकती है। |
|
3. Memory usage अधिक होती है। |
3.
Memory usage बहुत कम होती है। |
|
4. Data safe रहता है (overwrite नहीं होता)। |
4.
Data overwrite होने का खतरा। |
|
5. Size = सभी members के size का कुल योग। |
5.
Size = सबसे बड़े member के size के बराबर। |