[英]How to initialize this array of structures of array of structures?
I have the following code, which is not working as expected. 我有以下代码,它没有按预期工作。 It compiles, but throws a lot of warnings and segfaults when executed:
它编译,但执行时会抛出很多警告和段错误:
#include <stdio.h>
enum output {
A,
B,
C,
D,
};
struct translation {
char *from;
enum output to;
};
struct dictionary {
struct translation *foo;
struct translation *bar;
};
enum language {
ONE,
ANOTHER,
};
struct dictionary languages[] = {
[ONE] = {
.foo = {
{"LF", A},
{"LLF", C},
{"RRF", D},
},
.bar = {
{"L", B},
},
},
[ANOTHER] = {
.foo = {
{"FF", B},
{"RRF", D},
},
.bar = {
{"LF", B},
{"R", C},
{"RR", D},
},
},
};
int main(void)
{
printf("%s\n", languages[ONE].foo[0].from);
return 0;
}
I am probably initializing languages
the wrong way. 我可能错误地初始化
languages
。
languages
array in which I can access different dictionaries by language
: languages[ONE]
languages
数组,我可以通过language
访问不同的词典: languages[ONE]
languages[ONE].foo
languages[ONE].foo
Is that even possible? 这甚至可能吗? What am I doing wrong?
我究竟做错了什么?
When compiling with gcc
I get this (cropped) output: 当用
gcc
编译时,我得到这个(裁剪的)输出:
asdf.c:27:17: warning: braces around scalar initializer
.foo = {
^
asdf.c:27:17: note: (near initialization for ‘languages[0].foo’)
asdf.c:28:25: warning: braces around scalar initializer
{"LF", A},
^
asdf.c:28:25: note: (near initialization for ‘languages[0].foo’)
asdf.c:28:26: warning: initialization of ‘struct translation *’ from incompatible pointer type ‘char *’ [-Wincompatible-pointer-types]
[...]
The same warnings/notes repeat for multiple parts of the code. 对于代码的多个部分,重复相同的警告/注释。
Here are two things you can do: 您可以执行以下两项操作:
struct translation *foo;
struct translation *foo;
分配内存struct translation *foo;
and struct translation *bar;
struct translation *bar;
(you can also use malloc
to dynamically allocate memory). malloc
动态分配内存)。 For example: struct dictionary { struct translation foo[10]; struct translation bar[10]; };
struct dictionary languages[] = { [ONE] = { .foo = (struct translation []){ {"LF", A}, {"LLF", C}, {"RRF", D}, }, .bar = (struct translation []){ {"L", B}, }, }, [ANOTHER] = { .foo = (struct translation []){ {"FF", B}, {"RRF", D}, }, .bar = (struct translation []){ {"LF", B}, {"R", C}, {"RR", D}, }, }, };
Note 注意
As mentioned by @MM , adding the qualifier const
before struct dictionary
is a good idea if its values won't change during runtime. 正如@MM所提到的,如果在运行时它的值不会改变,那么在
struct dictionary
之前添加限定符const
是个好主意。
Just initialize each array separately: 只需分别初始化每个数组:
#include <stdio.h>
enum output
{
a
, b
, c
, d
};
struct translation
{
char const * from;
enum output to;
};
struct dictionary
{
struct translation * foo;
struct translation * bar;
};
enum language
{
one
, another
, languages_count
};
struct translation one_language_foo_translations[] =
{
{"LF" , a}
, {"LLF", c}
, {"RRF", d}
};
struct translation one_language_bar_translations[] =
{
{"L", b}
};
struct translation another_language_foo_translations[] =
{
{"FF" , b}
, {"RRF", d}
};
struct translation another_language_bar_translations[] =
{
{"LF", b}
, {"R" , c}
, {"RR", d}
};
struct dictionary languages[languages_count] =
{
{one_language_foo_translations, one_language_bar_translations}
, {another_language_foo_translations, another_language_bar_translations}
};
int main(void)
{
printf("%s\n", languages[one].foo[0].from);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.