0

A tool I use produces the following output:

#define VAR { {1, {2,3}},  {2, {5,1,2,3,4}},  {1, {1}} }

I want to use this in my program. The question is how to use it properly.

My first approach was something like this:

#define VAR { {1, {2,3}},  {2, {5,1,2,3,4}},  {1, {1}} }
#include <stdio.h>
typedef struct vars {
    int first;
    int second[];
} vars;

int main( int argc, char** argv ) {
   vars allvars[] = VAR;

   printf("%i\n",allvars[0].second[0]);
   return 0;
}

But gcc doesn't like it:

error: initialization of flexible array member in a nested context

My second approach was about a 3d-array:

#define VAR { {1, {2,3}},  {2, {5,1,2,3,4}},  {1, {1}} }
#include <stdio.h>

int main( int argc, char** argv ) {
   int allvars[3][2][2] = VAR;       
   printf("%i\n",allvars[1][1][3]);
   return 0;
}

Similar reaction of the compiler:

warning: braces around scalar initializer

Does anyone know how to deal with it? Is it even possible to use an initialiser list such as the given one or do anybody knows a nice hack to deal with this problem?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Hymir
  • 811
  • 1
  • 10
  • 20
  • possible duplicate of [Dynamic array and prebuilt data](http://stackoverflow.com/questions/16016627/dynamic-array-and-prebuilt-data) – R Sahu Feb 05 '15 at 19:25
  • Related: http://stackoverflow.com/questions/3875523/lookup-table-in-c. – R Sahu Feb 05 '15 at 19:26
  • Correct me if i am wrong but the ideas behind this solutions are about using malloc, arent they? But i dont want to use a malloc. Is there any possibility to avoid this? "no" is also an acceptable answer! :D – Hymir Feb 06 '15 at 12:23
  • You can't do what you are trying. The posts I linked to in my earlier comments are more proof that you can't do it. – R Sahu Feb 06 '15 at 16:35

0 Answers0