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.
- I would like to have that
languagesarray in which I can access different dictionaries bylanguage:languages[ONE] - I would like to access then different translation tables with the dictionary field:
languages[ONE].foo - All translation tables accessed with a language+field pair may have different array lengths, as shown in the code example
Is that even possible? What am I doing wrong?
When compiling with gcc I get this (cropped) output:
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.
[](no size) but that was illegal. You can post that as an answer if you want. Although I will wait before accepting to see if there is a way of making this work without having to specify a dictionary table size "big enough" to hold all translations.