这是我的部分代码。我只想将 arraylist[0]
初始化为 arraylist[0].x = 0
和 arraylist[0].y = 0
.我不需要初始化结构数组的其余部分。我该怎么做?谢谢。
#include <stdio.h>
struct example {
int x;
int y;
};
struct example arraylist[40];
int main(int argc, char *argv[]){
printf("%d\n %d\n", arraylist[0].x, arraylist[0].y);
return 0;
}
最佳答案
您可以初始化结构数组的任何特定元素。
例如:
struct example arraylist[40] = { [0]={0,0}}; //sets 0th element of struct
struct example arraylist[40] = { [5]={0,0}}; //sets 6th element of struct
这叫做 Designated Initializers在 C99 对其进行改编之前,它曾经是 GNU 扩展,并且自 C99 以来也受标准 C 支持。
关于c - C中的结构数组初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10665339/