App2015
App2015

Reputation: 993

struct definition within a struct

Is it possible to not only embed a structure but define within the structure as well in C?

struct Student { 
    char *name;
    struct Student *next;
};

struct School {
    struct Student *Students; // definition and embedding inline possible?
}

Upvotes: 1

Views: 59

Answers (1)

AnT stands with Russia
AnT stands with Russia

Reputation: 320361

It is possible to fully declare a struct type inside another struct type's declaration as long as you are immediately using the inner struct declaration to declare a field. Your declarations can be rewritten as

struct School {
  struct Student { 
    char *name;
    struct Student *next;
  } *Students;
};

struct Student is still a file-scope type, just as in your original code. And there's not much point in doing it that way since it is much less readable.

Upvotes: 4

Related Questions