C 嵌套数组';初始化?
我想转换此Python代码:C 嵌套数组';初始化?,c,arrays,struct,initialization,C,Arrays,Struct,Initialization,我想转换此Python代码: outsides = [ { 'baz': 7, 'foobar': [ {'bar': 2, 'foo': 1}, {'bar': 4, 'foo': 3}, {'bar': 6, 'foo': 5}]}, {'baz': 8}] 到目前为止,我提出了以下代码: #include <stdio.h> struct Insi
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;
}
#包括
内部结构{
int foo;
int-bar;
};
外部结构{
int baz;
内部常量结构*foobar;
};
内部常量结构[]={
{.foo=1,.bar=2},
{.foo=3,.bar=4},
{.foo=5,.bar=6},
};
外部结构常数[]={
{.baz=7,.foobar=insides},
{.baz=8,.foobar=NULL},
};
int main(int argc,char*argv[])
{
printf(“外部:%i\n”,sizeof(外部)/sizeof(外部));
printf(“内部:%i\n”,大小为(外部[0].foobar));
返回0;
}
但有两个问题:
注意:这是一个简化的示例,演示了我的两个基本问题,但我不能使用灵活数组成员,因为外部结构中有多个灵活数组成员。当然,foobar成员的对象数量可能会有所不同。我需要一个C,而不是C++的解决方案。< p>回答问题1,因为你已经有指定的初始化器,你也有。但是,要回答问题2,您必须告诉代码指针指向的数组中有多少个条目。C不会为您记录信息。您可以使用sentinel值,也可以显式记录计数。不管怎样,你都必须这样做。C不会在幕后为你做任何事。同时,在内定义<代码>结构>内嵌代码<>结构> >,没有C++中的作用域,以防止在内使用<代码>结构>独立于<代码>结构> >,这样做没有益处——而且我将保持结构定义与您一样。 因此,您可以使用:
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 },
};
最后一个.num\u foobar=0
可以省略,因为未指定的成员无论如何都初始化为零,但我不建议这样做。如果愿意,您可以使用int
代替size\t
。(上面的代码需要
或定义大小的其他标题之一(例如
)–但添加了\include
,它会为我干净地编译成目标代码。)
我错过了你的要求:
如果有另一个间接方法,那就太完美了:如果foobar
成员是指向内部元素的指针数组,那么我可以在末尾添加一个空指针
这可以通过结构中的类型更改和初始值设定项中的更多工作来完成。我认为上面的代码比较容易理解,但是下面的代码也不是很可怕
#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 },
};
#包括
内部结构
{
int foo;
int-bar;
};
外部结构
{
int baz;
**foobar内的const结构;
};
外部结构常数[]=
{
{.baz=7,
.foobar=&(*[]内部的常量结构){
&(内部结构){.foo=1,.bar=2},
&(内部结构){.foo=3,.bar=4},
&(内部结构){.foo=5,.bar=6},
无效的
})[0],
},
{.baz=8,.foobar=NULL},
};
虽然这是“可行的”,但我肯定更喜欢第一个代码。回答问题1,因为您有指定的初始值设定项,所以您也有。但是,要回答问题2,您必须告诉代码指针指向的数组中有多少个条目。C不会为您记录信息。您可以使用sentinel值,也可以显式记录计数。不管怎样,你都必须这样做。C不会在幕后为你做任何事。同时,在内定义<代码>结构>内嵌代码<>结构> >,没有C++中的作用域,以防止在内使用<代码>结构>独立于<代码>结构> >,这样做没有益处——而且我将保持结构定义与您一样。
因此,您可以使用:
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 },
};
最后一个.num\u foobar=0
可以省略,因为未指定的成员无论如何都初始化为零,但我不建议这样做。如果愿意,您可以使用int
代替size\t
。(上面的代码需要
或定义大小的其他标题之一(例如
)–但添加了\include
,它会为我干净地编译成目标代码。)
我错过了你的要求:
如果有另一个间接方法,那就太完美了:如果foobar
成员是指向内部元素的指针数组,那么我可以在末尾添加一个空指针
这可以通过结构中的类型更改和初始值设定项中的更多工作来完成。我认为上面的代码比较容易理解,但是下面的代码也不是很可怕
#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 },
};
#包括
内部结构
{
int foo;
int-bar;
};
外部结构
{
int baz;
**foobar内的const结构;
};
外部结构常数[]=
{
{.baz=7,
.foobar=&(*[]内部的常量结构){
&(内部结构){.foo=1,.bar=2},
&(内部结构){.foo=3,.bar=4},
&(内部结构){.foo=5,.bar=6},
无效的
})[0],
},
{.baz=8,.foobar=NULL},
};
虽然这是“可行的”