### 嵌套数组的初始化？

``````outsides = [
{   'baz': 7,
'foobar': [   {'bar': 2, 'foo': 1},
{'bar': 4, 'foo': 3},
{'bar': 6, 'foo': 5}]},
{'baz': 8}]
``````

``````#include <stdio.h>

struct Inside {
int foo;
int bar;
};

struct Outside {
int baz;
const struct Inside* foobar;
};

const struct Inside insides[] = {
{ .foo = 1, .bar = 2 },
{ .foo = 3, .bar = 4 },
{ .foo = 5, .bar = 6 },
};

const struct Outside outsides[] = {
{ .baz = 7, .foobar = insides },
{ .baz = 8, .foobar = NULL },
};

int main(int argc, char *argv[])
{
printf("outsides: %i\n", sizeof(outsides) / sizeof(Outside));
printf("insides: %i\n", sizeof(outsides[0].foobar));
return 0;
}
``````

1. 如何在外部声明中内联内部声明？
2. 如何确定foobar成员的内部元素数量？

#### 1 个答案:

``````struct Inside
{
int foo;
int bar;
};

struct Outside
{
int baz;
size_t num_foobar;
const struct Inside* foobar;
};

const struct Outside outsides[] =
{
{ .baz = 7, .num_foobar = 3,
.foobar = (struct Inside[]){
{ .foo = 1, .bar = 2 },
{ .foo = 3, .bar = 4 },
{ .foo = 5, .bar = 6 },
}
},
{ .baz = 8, .num_foobar = 0, .foobar = NULL },
};
``````

``````#include <stddef.h>

struct Inside
{
int foo;
int bar;
};

struct Outside
{
int baz;
const struct Inside **foobar;
};

const struct Outside outsides[] =
{
{ .baz = 7,
.foobar = &((const struct Inside*[]){
&(struct Inside){ .foo = 1, .bar = 2 },
&(struct Inside){ .foo = 3, .bar = 4 },
&(struct Inside){ .foo = 5, .bar = 6 },
NULL,
})[0],
},
{ .baz = 8, .foobar = NULL },
};
``````