自己参照構造体定義?

終わりのない再帰になるため、Cell に別のセルを含めることはできません。

ただし、Cell には別のセルへのポインターを含めることができます。

typedef struct Cell {
  bool isParent;
  struct Cell* child;
} Cell;

C では、構造体自体で作成している typedef を参照することはできません。次のテスト プログラムのように、構造名を使用する必要があります:

#include <stdio.h>
#include <stdlib.h>

typedef struct Cell {
  int cellSeq;
  struct Cell* next; /* 'tCell *next' will not work here */
} tCell;

int main(void) {
    int i;
    tCell *curr;
    tCell *first;
    tCell *last;

    /* Construct linked list, 100 down to 80. */

    first = malloc (sizeof (tCell));
    last = first;
    first->cellSeq = 100;
    first->next = NULL;
    for (i = 0; i < 20; i++) {
        curr = malloc (sizeof (tCell));
        curr->cellSeq = last->cellSeq - 1;
        curr->next = NULL;
        last->next = curr;
        last = curr;
    }

    /* Walk the list, printing sequence numbers. */

    curr = first;
    while (curr != NULL) {
        printf ("Sequence = %d\n", curr->cellSeq);
        curr = curr->next;
    }

    return 0;
}

標準ではおそらくこれよりもはるかに複雑ですが、コンパイラが struct Cell を認識していると考えることができます。 typedef の最初の行 しかし、tCell については知りません 最後の行まで :-) それが私がそのルールを覚えている方法です.


理論的な観点から、言語は自己参照構造のみをサポートでき、自己包括的構造はサポートできません。