<分区>
有什么区别:
typedef struct
{
} hello;
和:
struct hello
{
};
对不起,如果这是一个愚蠢的问题,但我不明白有什么区别......
标签 c
<分区>
有什么区别:
typedef struct
{
} hello;
和:
struct hello
{
};
对不起,如果这是一个愚蠢的问题,但我不明白有什么区别......
最佳答案
typedef
是一个关键字,意思是“类型定义”。它不是结构的一部分。在您的第一个示例中,它使 hello
成为一种新类型,即 struct { ... }
第一个创建类型;第二个声明了一个名为 hello
的结构。
不同的是第一个创建了一个新的类型。第二个只声明一个结构。区别很细微,但是在 C
中,如果没有 struct
关键字,就不能重用结构:(在 C++ 中,作用域规则不同。)
要使用类型,写这样的东西:
typedef struct {
int x, y;
} hello;
hello a, b, c;
这将创建三个类型均为 hello
的变量。
要使用该结构,请这样写:
struct xyz {
int z;
};
struct xyz d, e, f;
关于c - struct中 "typedef"是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27973962/