我什么时候应该将尾随添加_t到typedef'ed类型?
例如,我应该这样做:
typedef struct image image_t;
Run Code Online (Sandbox Code Playgroud)
或这个:
typedef struct image image;
Run Code Online (Sandbox Code Playgroud)
一般规则是什么?
另一个例子,我应该这样做:
typdef enum { ARRAY_CLOSED, ARRAY_OPEN, ARRAY_HALFOPEN } array_type_t;
Run Code Online (Sandbox Code Playgroud)
或这个:
typdef enum { ARRAY_CLOSED, ARRAY_OPEN, ARRAY_HALFOPEN } array_type;
Run Code Online (Sandbox Code Playgroud)
请赐教.
谢谢,Boda Cydo.
具体来说,是下面的代码,标记下方的行,好吗?
struct S{
int a;
};
#include <stdlib.h>
int main(){
struct S *p;
p = malloc(sizeof(struct S) + 1000);
// This line:
*(&(p->a) + 1) = 0;
}
Run Code Online (Sandbox Code Playgroud)
人们在这里争论,但没有人给出令人信服的解释或参考.
他们的论点略有不同,但基本相同
typedef struct _pack{
int64_t c;
} pack;
int main(){
pack *p;
char str[9] = "aaaaaaaa"; // Input
size_t len = offsetof(pack, c) + (strlen(str) + 1);
p = malloc(len);
// This line, with similar intention:
strcpy((char*)&(p->c), str);
// ^^^^^^^
Run Code Online (Sandbox Code Playgroud)