c - C中的结构数组初始化

标签 c arrays struct initialization

这是我的部分代码。我只想将 arraylist[0] 初始化为 arraylist[0].x = 0arraylist[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/

相关文章:

arrays - Swift 3 二维 Int 数组

c - 在 C 中为结构类型分配内存

c - 内核编译错误

c++ - 在 C++ 中打印 9x9 数组而不是 10x10 数组?

c - 为什么 ftell 会跳过文件中的某些位置?

c++ - 如何测量C++中的内存分配时间?

swift - Apple 对多线程引用和值类型的描述

json - 戈朗 : Validate Struct field of type string to be one of specific values

c - 获取正确的参数指针

c - 我需要帮助来使用 OpenMP 并行化此代码