gpt4 book ai didi

c - 在结构内部设置 typedef

转载 作者:太空宇宙 更新时间:2023-11-04 01:41:39 24 4
gpt4 key购买 nike

我想让我的代码更易于阅读,所以我想替换一个大的结构集,使其更具表现力,但它无法编译。

typedef float vec_t;
typedef vec_t vec3_t[3];

typedef struct{
int x;
vec3_t point;
} structure1;

//This Works just fine and is what i want to avoid
structure1 structarray[] = {
1,
{1,1,1}
};

//This is what i want to do but dont work
//error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
structarray[0].x = 1;
structarray[0].point = {0,0,0};

int main()
{
//This is acceptable and works
structarray[0].x = 1;


//but this dont work
//GCC error: expected expression before '{' token
structarray[0].point = {1,1,1};
}

为什么不编译?

最佳答案

structure1 structarray[] = {
[0].x = 1,
[0].point = { 0, 0, 0 },
};

// you can also use "compound literals" ...

structure1 f(void) {
return (structure1) { 1, { 2, 3, 4 }};
}

关于c - 在结构内部设置 typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4743276/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com