gpt4 book ai didi

c - 结构体中的算术运算

转载 作者:太空狗 更新时间:2023-10-29 16:07:55 25 4
gpt4 key购买 nike

在C语言中是否可以这样:

typedef struct S {
char a;
int b;
int c = this.a * 60 + this.b;
} S;

这种类型的结构在我的代码中非常有用。

最佳答案

不可以,在 C 中你不能在声明中分配内存值,这将是编译时错误。

但我可以建议一个解决方案(可能你喜欢):

#define INIT(this, a, b)  {a, b, (this).a * 60 + (this).b}

并调用它:

S s = INIT(s, '5', 6);

关于c - 结构体中的算术运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17784475/

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