Определение структуры внутри структуры

Обновить

December 2018

Просмотры

44 раз

1

Можно не только вставлять структуру, но определить в структуре, а в C?

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

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

1 ответы

4

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.

AnT