Why should we typedef a struct so often in C?

后端 未结 15 2927
無奈伤痛
無奈伤痛 2020-11-21 23:58

I have seen many programs consisting of structures like the one below

typedef struct 
{
    int i;
    char k;
} elem;

elem user;

Why is i

15条回答
  •  遥遥无期
    2020-11-22 00:04

    typedef will not provide a co-dependent set of data structures. This you cannot do with typdef:

    struct bar;
    struct foo;
    
    struct foo {
        struct bar *b;
    };
    
    struct bar {
        struct foo *f;
    };
    

    Of course you can always add:

    typedef struct foo foo_t;
    typedef struct bar bar_t;
    

    What exactly is the point of that?

提交回复
热议问题