相关疑难解决方法(0)

如果我在C或C++中使用`typedef`,何时应该在typedef'ed类型的末尾添加`_t`?

我什么时候应该将尾随添加_ttypedef'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.

c c++ typedef

48
推荐指数
4
解决办法
1万
查看次数

是否可以通过成员地址访问结构的大小,并分配足够的空间?

具体来说,是下面的代码,标记下方的行,好吗?

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)

c struct access-violation language-lawyer

21
推荐指数
2
解决办法
1594
查看次数

标签 统计

c ×2

access-violation ×1

c++ ×1

language-lawyer ×1

struct ×1

typedef ×1