C : typedef struct name {…}; VS typedef struct{…} name;

后端 未结 6 764
陌清茗
陌清茗 2020-11-29 15:59

As the title says, I have this code:

    typedef struct Book{
        int id;
        char title[256];
        char summ         


        
6条回答
  •  半阙折子戏
    2020-11-29 16:35

    Want to add by clarifying when you actually declare a variable.

    struct foo {
       int a;
    } my_foo;
    

    defines foo and immediately declares a variable my_foo of the struct foo type, meaning you can use it like this my_foo.a = 5;

    However, because typedef syntax follows typedef

    typedef struct bar {
       int b;
    } my_bar;
    

    is not declaring a variable my_bar of type struct bar, my_bar.b = 5; is illegal. It is instead giving a new name to the struct bar type in the form of my_bar. You can now declare the struct bar type with my_bar like this:

    my_bar some_bar;
    

提交回复
热议问题