self referential struct definition?

后端 未结 9 1489
死守一世寂寞
死守一世寂寞 2020-11-22 11:09

I haven\'t been writing C for very long, and so I\'m not sure about how I should go about doing these sorts of recursive things... I would like each cell to contain another

相关标签:
9条回答
  • 2020-11-22 11:48

    Clearly a Cell cannot contain another cell as it becomes a never-ending recursion.

    However a Cell CAN contain a pointer to another cell.

    typedef struct Cell {
      bool isParent;
      struct Cell* child;
    } Cell;
    
    0 讨论(0)
  • 2020-11-22 11:52

    Lets go through basic definition of typedef. typedef use to define an alias to an existing data type either it is user defined or inbuilt.

    typedef <data_type> <alias>;
    

    for example

    typedef int scores;
    
    scores team1 = 99;
    

    Confusion here is with the self referential structure, due to a member of same data type which is not define earlier. So In standard way you can write your code as :-

    //View 1
    typedef struct{ bool isParent; struct Cell* child;} Cell;
    
    //View 2
    typedef struct{
      bool isParent;
      struct Cell* child;
    } Cell;
    
    //Other Available ways, define stucture and create typedef
    struct Cell {
      bool isParent;
      struct Cell* child;
    };
    
    typedef struct Cell Cell;
    

    But last option increase some extra lines and words with usually we don't want to do (we are so lazy you know ;) ) . So prefer View 2.

    0 讨论(0)
  • 2020-11-22 11:54

    All previous answers are great , i just thought to give an insight on why a structure can't contain an instance of its own type (not a reference).

    its very important to note that structures are 'value' types i.e they contain the actual value, so when you declare a structure the compiler has to decide how much memory to allocate to an instance of it, so it goes through all its members and adds up their memory to figure out the over all memory of the struct, but if the compiler found an instance of the same struct inside then this is a paradox (i.e in order to know how much memory struct A takes you have to decide how much memory struct A takes !).

    But reference types are different, if a struct 'A' contains a 'reference' to an instance of its own type, although we don't know yet how much memory is allocated to it, we know how much memory is allocated to a memory address (i.e the reference).

    HTH

    0 讨论(0)
  • 2020-11-22 11:57

    From the theoretical point of view, Languages can only support self-referential structures not self-inclusive structures.

    0 讨论(0)
  • 2020-11-22 12:02

    There is sort of a way around this:

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

    If you declare it like this, it properly tells the compiler that struct Cell and plain-ol'-cell are the same. So you can use Cell just like normal. Still have to use struct Cell inside of the initial declaration itself though.

    0 讨论(0)
  • 2020-11-22 12:09

    In C, you cannot reference the typedef that you're creating withing the structure itself. You have to use the structure name, as in the following test program:

    #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;
    }
    

    Although it's probably a lot more complicated than this in the standard, you can think of it as the compiler knowing about struct Cell on the first line of the typedef but not knowing about tCell until the last line :-) That's how I remember that rule.

    0 讨论(0)
提交回复
热议问题