C中的结构数组

000*_*000 4 c arrays structure

对于我的生活,我无法弄清楚在C中创建结构数组的正确语法.我试过这个:

struct foo {
    int x;
    int y;
} foo[][] = {

    {
        { 1, 2 },
        { 4, 5 },
        { -1, -1 }
    },

    {
        { 55, 44 }
        { 100, 200 },
    }
};
Run Code Online (Sandbox Code Playgroud)

所以例如foo [1] [0] .x == 100,foo [0] [1] .y == 5等等.但是GCC吐出了很多错误.

如果有人能提供合适的语法,那将是伟大的.

编辑:好的,我试过这个:

struct foo {
     const char *x;
     int y;
};

struct foo bar[2][] = {

     {
      { "A", 1 },
      { "B", 2 },
      { NULL, -1 },
     },

     {
      { "AA", 11 },
      { "BB", 22 },
      { NULL, -1 },
     },

     {
      { "ZZ", 11 },
      { "YY", 22 },
      { NULL, -1 },
     },

     {
      { "XX", 11 },
      { "UU", 22 },
      { NULL, -1 },
     },
};
Run Code Online (Sandbox Code Playgroud)

但GCC给了我"数组条的元素有不完整的类型"和"数组初始化程序中的多余元素".

Mat*_*hen 7

这将创建并初始化二维结构数组,每行包含三个.请注意,您尚未提供初始化程序array[1][2],在这种情况下,它的内容未定义.

struct foo {
    const char *x;
    int y;
};

int main()
{
    struct foo array[][3] = {
        {
            { "foo", 2 },
            { "bar", 5 },
            { "baz", -1 },
        },
        {
            { "moo", 44 },
            { "goo", 200 },
        }
    };
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编辑:指向const字符串的x指针.尝试使您的示例接近您的真实代码.