perhaps? 2009-11-04 17:21 采纳率: 100%
浏览 748
已采纳

Typedef struct 对 struct 定义[重复]

This question already has an answer here:

I'm a beginner in C programming, but I was wondering what's the difference between using typedef when defining a structure versus not using typedef. It seems to me like there's really no difference, they accomplish the same.

struct myStruct{
    int one;
    int two;
};

vs.

typedef struct{
    int one;
    int two;
}myStruct;
</div>

转载于:https://stackoverflow.com/questions/1675351/typedef-struct-vs-struct-definitions

  • 写回答

12条回答 默认 最新

  • 关注

    The common idiom is using both:

    typedef struct X { 
        int x; 
    } X;
    

    They are different definitions. To make the discussion clearer I will split the sentence:

    struct S { 
        int x; 
    };
    
    typedef struct S S;
    

    In the first line you are defining the identifier S within the struct name space (not in the C++ sense). You can use it and define variables or function arguments of the newly defined type by defining the type of the argument as struct S:

    void f( struct S argument ); // struct is required here
    

    The second line adds a type alias S in the global name space and thus allows you to just write:

    void f( S argument ); // struct keyword no longer needed
    

    Note that since both identifier name spaces are different, defining S both in the structs and global spaces is not an error, as it is not redefining the same identifier, but rather creating a different identifier in a different place.

    To make the difference clearer:

    typedef struct S { 
        int x; 
    } T;
    
    void S() { } // correct
    
    //void T() {} // error: symbol T already defined as an alias to 'struct S'
    

    You can define a function with the same name of the struct as the identifiers are kept in different spaces, but you cannot define a function with the same name as a typedef as those identifiers collide.

    In C++, it is slightly different as the rules to locate a symbol have changed subtly. C++ still keeps the two different identifier spaces, but unlike in C, when you only define the symbol within the class identifier space, you are not required to provide the struct/class keyword:

     // C++
    struct S { 
        int x; 
    }; // S defined as a class
    
    void f( S a ); // correct: struct is optional
    

    What changes are the search rules, not where the identifiers are defined. The compiler will search the global identifier table and after S has not been found it will search for S within the class identifiers.

    The code presented before behaves in the same way:

    typedef struct S { 
        int x; 
    } T;
    
    void S() {} // correct [*]
    
    //void T() {} // error: symbol T already defined as an alias to 'struct S'
    

    After the definition of the S function in the second line, the struct S cannot be resolved automatically by the compiler, and to create an object or define an argument of that type you must fall back to including the struct keyword:

    // previous code here...
    int main() {
        S(); 
        struct S s;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(11条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料