C structs don't define types?

前端 未结 10 2207
离开以前
离开以前 2021-01-21 06:54

I\'ve just started learning C with a professional Java background and some (if no too much) C++ knowledge, and I was astonished that this doesn\'t work in C:

str         


        
10条回答
  •  不思量自难忘°
    2021-01-21 07:06

    In C, structs can have two kinds of names: a tag name, and a type name. A tag name can only be used if it's prefixed with struct, like:

    struct mytag { int a; }
    struct mytag mystruct;
    

    A typedef allows you to define both a tag name and a type name. The type name can be used without a struct prefix:

    typedef struct mytag { int a; } mytype;
    mytype mystruct; // No struct required
    

提交回复
热议问题