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

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

问题描述

我在C编程初学者,但我想知道什么是确定的结构与不使用的typedef在使用的typedef之间的差异。在我看来,像真的没有区别,他们完成相同的。

 结构MYSTRUCT {
    诠释之一;
    诠释2;
};

VS

  typedef结构{
    诠释之一;
    诠释2;
} MYSTRUCT;


解决方案

常见的成语同时使用:

  typedef结构X {
    INT X;
} X;

它们是不同的定义。为了使讨论更加清楚我会分裂了一句:

 的struct {
    INT X;
};类型定义的struct S;

在第一行正在定义的标识符取值的结构命名空间(而不是在C ++意义上)之内。您可以使用它,通过定义参数的类型为的struct 定义新定义类型的变量或函数的参数:

 无效F(结构的说法); //这里需要结构

第二行增加了在全局命名空间类型别名取值,从而允许你这样写:

 无效F(的说法); // struct关键字不再需要

请注意,由于这两个标识符名称空间是不同的,定义取值无论是在结构和全球空间是不是一个错误,因为它是不重新定义相同的标识符,但在不同的地方,而创建一个不同的标识符。

要赚差价更清楚:

 的typedef的struct {
    INT X;
}笔;无效S(){} //正确//无效T(){} //错误:符号为T已定义为一个别名为结构S'

您可以定义与结构的名称相同的功能标识符都保存在不同的空间,但不能使用相同的名称作为定义一个函数的typedef 因为这些标识符发生冲突。

在C ++中,它是略有不同的规则来定位一个符号巧妙地改变了。 C ++仍保持着两个不同的标识符空间,但与C,当你只定义在类标识符空间内的符号,你是不是需要提供结构/ class关键字:

  // C ++
的struct {
    INT X;
}; // s定义为一类无效F(S A); //正确的:结构是可选

什么改变搜索规则,而不是在这里被定义的标识符。编译器将搜索全局识别符表和取值还没有被发现后,将在类标识符内的取值搜索

在psented的code $ P $的行为以同样的方式:

 的typedef的struct {
    INT X;
}笔;无效S(){} //正确的[*]//无效T(){} //错误:符号为T已定义为一个别名为结构S'

在第二行中的取值函数的定义中,struct S能不能由编译器自动解决后,并创建一个对象或定义的那一个参数输入您必须退回到包括结构关键字:

  // previous code这里...
诠释主(){
    S();
    的struct S;
}

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;

解决方案

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

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

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