codegurukool.com · Free coding tutorials & online compiler

Home › C Tutorial › Nested structs and arrays of structs

Nested structs and arrays of structs

Structs inside structs, and lists of them.

Fields can be other structs, and you can make arrays of structs.

Example code

#include <stdio.h>
struct Pt { int x, y; };
struct Line { struct Pt a, b; };
int main() {
  struct Line l = {{0, 0}, {3, 4}};
  struct Pt p[2] = {{1, 2}, {5, 6}};
  printf("%d %d\n", l.b.y, p[1].x);
  return 0;
}

Run this code in the online editor

← C structuresStructs and pointers →