C Structures परिचय
In this page:
struct StructName {
data_type member1;
data_type member2;
};
struct StructName variable_name = {value1, value2};
variable_name.member1;
Structure क्या है?
एक array के उलट, जिसे हर element same type का होना ज़रूरी है, एक structure एक int, एक float, और एक char array को एक logical unit में मिला सकता है -- एक name, age, और GPA वाले student record जैसी चीज़ model करने के लिए ideal।
उदाहरण: What is a Structure?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Student
struct Student {
int age;
float gpa;
char grade;
};
// Program execution starts in main()
int main() {
struct Student s = {20, 3.5f, 'A'};
// Print formatted text to the screen
printf("%d %.1f %c", s.age, s.gpa, s.grade);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक Structure Define करना
braces के अंदर list किए गए members structure का blueprint बन जाते हैं -- जब तक आप उस structure type का एक variable declare न करें कोई memory allocate नहीं होती, ठीक वैसे जैसे एक class declare करने से एक object नहीं बनता।
उदाहरण: Defining a Structure
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Point
struct Point {
int x;
int y;
};
// Program execution starts in main()
int main() {
struct Point p;
p.x = 3;
p.y = 4;
// Print formatted text to the screen
printf("%d %d", p.x, p.y);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Structure Variables Declare करना
यह two-step process (struct define करना, फिर इसका एक variable declare करना) एक single statement में combine किया जा सकता है, लेकिन उन्हें अलग-अलग लिखना अक्सर बड़े programs को follow करना आसान बनाता है, खासकर जब एक struct एक header file में defined हो।
उदाहरण: Declaring Structure Variables
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Point
struct Point {
int x;
int y;
};
// Program execution starts in main()
int main() {
struct Point p1;
p1.x = 1;
p1.y = 2;
// Print formatted text to the screen
printf("%d %d", p1.x, p1.y);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Structure Members Access करना
student.age = 20 exactly एक normal variable की तरह age field पढ़ता और लिखता है -- dot operator वह है जो memory में एक specific member नाम को एक specific structure variable से जोड़ता है।
उदाहरण: Accessing Structure Members
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Student
struct Student {
int age;
};
// Program execution starts in main()
int main() {
struct Student student;
student.age = 20;
// Print formatted text to the screen
printf("%d", student.age);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Structures का Array
यह structured records की एक list model करने का natural तरीका है, जैसे students का एक roster, क्योंकि हर array element structure की अपनी member values के सेट के साथ एक पूरी independent copy है।
उदाहरण: Array of Structures
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Student
struct Student {
int age;
};
// Program execution starts in main()
int main() {
struct Student roster[2] = {{20}, {21}};
// Print formatted text to the screen
printf("%d %d", roster[0].age, roster[1].age);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक struct के closing brace के बाद semicolon भूल जाना।
- दो structs को
==से compare करना, जो allowed नहीं है। - बिना
typedefके एक variable declare करते समयstructkeyword भूल जाना।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: