typedef 结构与结构定义 [英] typedef struct vs struct definitions

查看:20
本文介绍了typedef 结构与结构定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 C 编程的初学者,但我想知道在定义结构时使用 typedef 与不使用 typedef 有什么区别.在我看来真的没有区别,他们实现了相同的目标.

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 goal.

struct myStruct{
    int one;
    int two;
};

对比

typedef struct{
    int one;
    int two;
}myStruct;

推荐答案

常见的习惯用法是同时使用两者:

The common idiom is using both:

typedef struct S { 
    int x; 
} S;

它们是不同的定义.为了使讨论更清楚,我将句子分开:

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

struct S { 
    int x; 
};

typedef struct S S;

在第一行中,您在结构名称空间中定义标识符 S(不是 C++ 意义上的).您可以使用它并通过将参数的类型定义为 struct 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

第二行在全局命名空间中添加了一个类型别名S,这样你就可以这样写:

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

请注意,由于两个标识符名称空间不同,因此在结构体和全局空间中定义 S 不是错误,因为它不是重新定义相同的标识符,而是在一个不同的地方.

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.

为了使区别更清楚:

typedef struct S { 
    int x; 
} T;

void S() { } // correct

//void T() {} // error: symbol T already defined as an alias to 'struct S'

您可以定义一个与结构体同名的函数,因为标识符保存在不同的空间中,但您不能定义一个与typedef同名的函数,因为这些标识符会发生冲突.

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.

在 C++ 中,它略有不同,因为定位符号的规则发生了微妙的变化.C++ 仍然保留了两个不同的标识符空间,但与 C 不同的是,当您只在类标识符空间内定义符号时,不需要提供 struct/class 关键字:

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

更改的是搜索规则,而不是定义标识符的位置.编译器将搜索全局标识符表,在没有找到 S 后,它将在类标识符中搜索 S.

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'

在第二行定义S函数后,S结构体不能被编译器自动解析,并且无法创建对象或定义参数您必须回退到包含 struct 关键字的那种类型:

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;
}

这篇关于typedef 结构与结构定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆