self referential struct definition?

后端 未结 9 1498
死守一世寂寞
死守一世寂寞 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: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  ;
    

    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.

提交回复
热议问题