← Back to C Course | Chapter 8: Structures & Unions | Lesson 1 of 7

C Structures परिचय

एक structure कई related values को एक नाम के तहत group करता है, name, age, और grade के लिए blanks वाले एक form की तरह। यह information को bundle करने का एक simple तरीका है।
Syntax
c
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?

c
// 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;
}

एक Structure Define करना

braces के अंदर list किए गए members structure का blueprint बन जाते हैं -- जब तक आप उस structure type का एक variable declare न करें कोई memory allocate नहीं होती, ठीक वैसे जैसे एक class declare करने से एक object नहीं बनता।

उदाहरण: Defining a Structure

c
// 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;
}

Structure Variables Declare करना

यह two-step process (struct define करना, फिर इसका एक variable declare करना) एक single statement में combine किया जा सकता है, लेकिन उन्हें अलग-अलग लिखना अक्सर बड़े programs को follow करना आसान बनाता है, खासकर जब एक struct एक header file में defined हो।

उदाहरण: Declaring Structure Variables

c
// 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;
}

Structure Members Access करना

student.age = 20 exactly एक normal variable की तरह age field पढ़ता और लिखता है -- dot operator वह है जो memory में एक specific member नाम को एक specific structure variable से जोड़ता है।

उदाहरण: Accessing Structure Members

c
// 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;
}

Structures का Array

यह structured records की एक list model करने का natural तरीका है, जैसे students का एक roster, क्योंकि हर array element structure की अपनी member values के सेट के साथ एक पूरी independent copy है।

उदाहरण: Array of Structures

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक struct के closing brace के बाद semicolon भूल जाना।
  2. दो structs को == से compare करना, जो allowed नहीं है।
  3. बिना typedef के एक variable declare करते समय struct keyword भूल जाना।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.