这个C结构定义的优点是什么?
What is the advantage of this C structure definition?
不太明白为什么要这样定义这个结构体。
这里是有问题的代码块...
typedef struct Except_Frame Except_Frame;
struct Except_Frame {
Except_Frame *prev;
jmp_buf env;
const char *file;
int line;
const T *exception;
};
为什么这个结构是这样定义的,而不是仅仅...
typedef struct {
Except_Frame *prev;
jmp_buf env;
const char *file;
int line;
const T *exception;
} Except_Frame;
又有什么优势呢?
如果你不使用:
typedef struct Except_Frame Except_Frame;
那么,struct
需要使用以下方式定义:
struct Except_Frame {
// The keyword struct is necessary without the typedef
struct Except_Frame *prev;
jmp_buf env;
const char *file;
int line;
const T *exception;
};
如果你想在一个语句中定义 struct
和 typedef
,它将是:
typedef struct Except_Frame {
// The keyword struct is necessary without the typedef
// being defined ahead of the definition of the struct.
struct Except_Frame *prev;
jmp_buf env;
const char *file;
int line;
const T *exception;
} Except_Frame;
通过使用
typedef struct Except_Frame Except_Frame;
您正在将结构 "struct Except_Frame" 重命名为 "Except_Frame"。
首先,输入Except_Frame比结构Except_Frame更方便。
其次,在这种情况下,结构的字段 "Except_Frame *prev" 将编译失败,因为编译器不熟悉名为 "Except_Frame" 的结构(它熟悉名为 struct Except_Frame 的结构)
干杯,
N
如果您需要那个 typedef 名称,那么您可以使用的两种流行的文体变体实际上如下所示
typedef struct Except_Frame Except_Frame;
struct Except_Frame {
Except_Frame *prev;
...
};
或
typedef struct Except_Frame {
struct Except_Frame *prev;
...
} Except_Frame;
请注意与您的第二个变体的区别(您原来的第二个变体甚至无法编译)。
现在,您想使用哪一个很大程度上取决于您的个人喜好。第一个变体使类型名称的 "short" 版本(仅 Except_Frame
)早于第二个变体可用。
首先我们要了解typedef的使用; typedef 可用于指示变量如何表示某物;例子
typedef int km_per_hour ;
typedef int points ;
现在,谈到你的情况,你正在定义结构,你希望它通过 typedef 来调用;我们需要在使用它之前预先定义它,因此我们在定义结构
之前声明
1 typedef struct Except_Frame t_Except_Frame;
2 struct Except_Frame {
3 t_Except_Frame *prev;
4 ...
5 }
行 1) 现在编译器知道会有名称为 "struct Except_Frame" 的结构,我们需要将类型定义为 "t_Except_Frame";您是否注意到我为 typedef 添加了 t_;遵循这一点是一种很好的做法,这样程序员就可以很容易地理解这个值是 typedef;
行 3) 系统理解它是 struct Except_Frame 的 typedef 变量并据此编译程序。
不太明白为什么要这样定义这个结构体。
这里是有问题的代码块...
typedef struct Except_Frame Except_Frame;
struct Except_Frame {
Except_Frame *prev;
jmp_buf env;
const char *file;
int line;
const T *exception;
};
为什么这个结构是这样定义的,而不是仅仅...
typedef struct {
Except_Frame *prev;
jmp_buf env;
const char *file;
int line;
const T *exception;
} Except_Frame;
又有什么优势呢?
如果你不使用:
typedef struct Except_Frame Except_Frame;
那么,struct
需要使用以下方式定义:
struct Except_Frame {
// The keyword struct is necessary without the typedef
struct Except_Frame *prev;
jmp_buf env;
const char *file;
int line;
const T *exception;
};
如果你想在一个语句中定义 struct
和 typedef
,它将是:
typedef struct Except_Frame {
// The keyword struct is necessary without the typedef
// being defined ahead of the definition of the struct.
struct Except_Frame *prev;
jmp_buf env;
const char *file;
int line;
const T *exception;
} Except_Frame;
通过使用
typedef struct Except_Frame Except_Frame;
您正在将结构 "struct Except_Frame" 重命名为 "Except_Frame"。
首先,输入Except_Frame比结构Except_Frame更方便。 其次,在这种情况下,结构的字段 "Except_Frame *prev" 将编译失败,因为编译器不熟悉名为 "Except_Frame" 的结构(它熟悉名为 struct Except_Frame 的结构)
干杯, N
如果您需要那个 typedef 名称,那么您可以使用的两种流行的文体变体实际上如下所示
typedef struct Except_Frame Except_Frame;
struct Except_Frame {
Except_Frame *prev;
...
};
或
typedef struct Except_Frame {
struct Except_Frame *prev;
...
} Except_Frame;
请注意与您的第二个变体的区别(您原来的第二个变体甚至无法编译)。
现在,您想使用哪一个很大程度上取决于您的个人喜好。第一个变体使类型名称的 "short" 版本(仅 Except_Frame
)早于第二个变体可用。
首先我们要了解typedef的使用; typedef 可用于指示变量如何表示某物;例子
typedef int km_per_hour ;
typedef int points ;
现在,谈到你的情况,你正在定义结构,你希望它通过 typedef 来调用;我们需要在使用它之前预先定义它,因此我们在定义结构
之前声明1 typedef struct Except_Frame t_Except_Frame;
2 struct Except_Frame {
3 t_Except_Frame *prev;
4 ...
5 }
行 1) 现在编译器知道会有名称为 "struct Except_Frame" 的结构,我们需要将类型定义为 "t_Except_Frame";您是否注意到我为 typedef 添加了 t_;遵循这一点是一种很好的做法,这样程序员就可以很容易地理解这个值是 typedef;
行 3) 系统理解它是 struct Except_Frame 的 typedef 变量并据此编译程序。