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?

4
  • possible duplicate of Dynamic array and prebuilt data Commented Feb 5, 2015 at 19:25
  • Related: stackoverflow.com/questions/3875523/lookup-table-in-c. Commented Feb 5, 2015 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 Commented Feb 6, 2015 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. Commented Feb 6, 2015 at 16:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.