Why should we typedef a struct so often in C?

后端 未结 15 2897
無奈伤痛
無奈伤痛 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:25

    Using a typedef avoids having to write struct every time you declare a variable of that type:

    struct elem
    {
     int i;
     char k;
    };
    elem user; // compile error!
    struct elem user; // this is correct
    

提交回复
热议问题