[英]In C, how do I "directly" copy a struct to an array member?
So I want to do this:所以我想这样做:
struct element{
int t;
};
int main(int argc, char *argv[]) {
struct element arr[10];
arr[0]={3};
return 0;
}
But this gives me the following error:但这给了我以下错误:
test.c: In function ‘main’:
test.c:7:10: error: expected expression before ‘{’ token
7 | arr[0]={3};
| ^
To fix this, I will have to resort to writing:为了解决这个问题,我将不得不求助于写作:
int main(int argc, char *argv[]) {
struct element arr[10];
struct element tmp = {3};
arr[0]= tmp;
return 0;
}
But I think it is inelegant to write a tmp value when I just want to assign directly to element{3}
.但是我认为当我只想直接分配给element{3}
时,写一个 tmp 值是不雅的。 How do I fix the syntax so that I do not need to create this tmp
value?如何修复语法以便我不需要创建此tmp
值?
你想要的是一个复合文字:
arr[0]=(struct element){3};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.