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条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题