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

C Nested Structures

A nested structure is a structure that has another structure inside, like a form with a smaller form attached. You reach the inner parts one level at a time.
Syntax
c
struct Inner {
  data_type member;
};

struct Outer {
  struct Inner inner_name;
};

outer_variable.inner_name.member;

What is a Nested Structure?

For example, an Employee structure might contain an Address structure as one of its fields, letting you model has-a relationships between related pieces of data cleanly, similar to composition in object-oriented languages.

Example: 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;
}

Defining Nested Structures

The inner structure type must be fully defined (or at least declared) before the outer structure references it, since the compiler needs to know the inner structure's size to correctly lay out the outer structure in memory.

Example: 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;
}

Initializing Nested Structures

A nested initializer looks like Employee e = {"Alice", {"123 Main St", "Springfield"}} -- the inner braces group the Address fields separately from the outer Employee fields, mirroring the nesting in the type definition.

Example: 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;
}

Accessing Nested Members

employee.address.city reads the city field by first navigating into the address member, then into its city field -- each dot operator moves one level deeper into the nested structure hierarchy.

Example: 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;
}

Nesting Arrays of Structures

This lets you model more complex real-world data, like a company with an array of departments, each containing an array of employee structures -- nesting arrays and structures together is how C represents hierarchical data without built-in classes.

Example: 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. #}
Common Mistakes
  1. Accessing an inner member without the full path, such as emp.city instead of emp.address.city.
  2. Leaving the inner struct uninitialized so its members contain garbage.
  3. Defining a struct that contains itself by value, which is not allowed; use a pointer instead.
🔒

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.