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

C में Nested Structures

एक nested structure एक structure है जिसके अंदर दूसरा structure है, एक छोटे form वाले एक बड़े form जैसा। आप inner parts तक एक-एक level पहुंचते हैं।
Syntax
c
struct Inner {
  data_type member;
};

struct Outer {
  struct Inner inner_name;
};

outer_variable.inner_name.member;

Nested Structure क्या है?

उदाहरण के लिए, एक Employee structure में इसकी एक field की तरह एक Address structure हो सकता है, आपको related data के pieces के बीच has-a relationships cleanly model करने देते हुए, object-oriented languages में composition जैसा।

उदाहरण: What is a Nested Structure?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Address
struct Address {
	char city[20];
};
// Define a structure type named Employee
struct Employee {
	char name[20];
	struct Address address;
};
// Program execution starts in main()
int main() {
	struct Employee e = {"Alice", {"Springfield"}};
	// Print formatted text to the screen
	printf("%s %s", e.name, e.address.city);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Nested Structures Define करना

outer structure के इसे reference करने से पहले inner structure type को पूरी तरह defined (या कम से कम declared) होना चाहिए, क्योंकि compiler को memory में outer structure को सही तरीके से lay out करने के लिए inner structure का size जानना ज़रूरी है।

उदाहरण: Defining Nested Structures

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Address
struct Address {
	char street[30];
};
// Define a structure type named Employee
struct Employee {
	char name[20];
	struct Address address;
};
// Program execution starts in main()
int main() {
	struct Employee e = {"Bob", {"123 Main St"}};
	// Print formatted text to the screen
	printf("%s", e.address.street);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Nested Structures Initialize करना

एक nested initializer ऐसा दिखता है Employee e = {"Alice", {"123 Main St", "Springfield"}} -- inner braces Address fields को outer Employee fields से अलग group करते हैं, type definition में nesting को mirror करते हुए।

उदाहरण: Initializing Nested Structures

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Address
struct Address {
	char street[30];
	char city[20];
};
// Define a structure type named Employee
struct Employee {
	char name[20];
	struct Address address;
};
// Program execution starts in main()
int main() {
	struct Employee e = {"Alice", {"123 Main St", "Springfield"}};
	// Print formatted text to the screen
	printf("%s", e.address.city);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Nested Members Access करना

employee.address.city पहले address member में navigate करके, फिर इसके city field में जाकर city field पढ़ता है -- हर dot operator nested structure hierarchy में एक level गहरा move करता है।

उदाहरण: Accessing Nested Members

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Address
struct Address {
	char city[20];
};
// Define a structure type named Employee
struct Employee {
	struct Address address;
};
// Program execution starts in main()
int main() {
	struct Employee employee = {{"Springfield"}};
	// Print formatted text to the screen
	printf("%s", employee.address.city);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Structures के Arrays को Nest करना

यह आपको ज़्यादा complex real-world data model करने देता है, जैसे departments के एक array वाली एक company, हर एक में employee structures का एक array -- arrays और structures को साथ nest करना यह है कि C बिना built-in classes के hierarchical data कैसे represent करता है।

उदाहरण: Nesting Arrays of Structures

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Employee
struct Employee {
	char name[20];
};
// Define a structure type named Department
struct Department {
	struct Employee employees[2];
};
// Program execution starts in main()
int main() {
	struct Department d = {{{"Alice"}, {"Bob"}}};
	// Print formatted text to the screen
	printf("%s %s", d.employees[0].name, d.employees[1].name);
	// 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. पूरे path के बिना एक inner member access करना, जैसे emp.address.city की बजाय emp.city।
  2. inner struct को uninitialized छोड़ देना ताकि इसके members garbage रखें।
  3. एक struct define करना जिसमें खुद value से शामिल हो, जो allowed नहीं है; इसकी बजाय एक pointer इस्तेमाल करें।
🔒

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.