C Nested Structures

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is Nested Structures?

A structure member can itself be a structure. Access inner members by chaining dots: rect.top_left.x. You may define the inner struct separately (clearer) or inline. A struct cannot contain itself, but it can contain a pointer to its own type.

Remember
struct Point { int x, y; };
struct Rect {
    struct Point top_left;
    struct Point bottom_right;
};
r.bottom_right.x    /* chain the dots */

Cheat Sheet

Keep these facts handy while you work with Nested Structures in C.

TopicDetail
Accessouter.inner.field
InitializeNested braces {{0, 0}, {4, 3}}
Self-referenceOnly through a pointer
StyleDefine inner types first

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Arrays of Structures.

Example 1 — Rectangle made of two points

Compute width, height, and area from the corners.

C
#include <stdio.h>

struct Point { int x, y; };
struct Rect  { struct Point tl, br; };

int main(void) {
    struct Rect r = {{0, 0}, {4, 3}};
    int w = r.br.x - r.tl.x;
    int h = r.br.y - r.tl.y;

    printf("width=%d height=%d area=%d\n", w, h, w * h);
    return 0;
}

How It Works

  1. tl and br are struct Point members.
  2. r.br.x reads the x of the bottom-right corner.
  3. Nested braces initialize the inner structs.

Edge Cases & Pitfalls

Check these before relying on Nested Structures in real programs.

self

A struct containing itself

struct Node { struct Node next; }; has infinite size. Use struct Node *next;.

order

Using a type before defining it

Define the inner struct above the outer one.

Key Takeaways

  • Chain: dots through levels.
  • Compose: build big types from small ones.
  • Pointer: for self-reference.

One line: A structure member can itself be a structure.

Frequently Asked Questions

Chain the dot operator, for example rect.top_left.x. With pointers, use ptr->top_left.x.
Yes. The array member can hold any complete struct type.
Continue with "C Arrays of Structures" next. Store many records in an array and loop over them. The C Structures, Unions and Enums hub lists every topic in order.

Next: Arrays of Structures

Store many records in an array and loop over them.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful