0

What is the correct syntax to initialize the following 2 level of nested arrays ?

z3 src # cat tc.c
#include <stdio.h>

typedef struct level1_t {
        char                           var[15];
        int                            value;
} level1_t;

typedef struct level2_t {
    level1_t     data[3];
} level2_t;

level2_t level2[1] = {
                [0]={
                        [0]={"one",1},
                        [1]={"two",2},
                        [2]={"three",3}
                }
};

int main() {

        printf("%s\n",level2[0].data[1].var);
}
z3 src # gcc -ggdb -o tc tc.c
tc.c:14:4: error: array index in non-array initializer
tc.c:14:4: error: (near initialization for 'level2[0]')
tc.c:15:4: error: array index in non-array initializer
tc.c:15:4: error: (near initialization for 'level2[0]')
tc.c:15:4: error: extra brace group at end of initializer
tc.c:15:4: error: (near initialization for 'level2[0]')
tc.c:15:4: warning: excess elements in struct initializer
tc.c:15:4: warning: (near initialization for 'level2[0]')
tc.c:16:4: error: array index in non-array initializer
tc.c:16:4: error: (near initialization for 'level2[0]')
tc.c:16:4: error: extra brace group at end of initializer
tc.c:16:4: error: (near initialization for 'level2[0]')
tc.c:16:4: warning: excess elements in struct initializer
tc.c:16:4: warning: (near initialization for 'level2[0]')
z3 src #
1
  • Just as a warning, this syntax is only available in C ISO C99 (not C++). Commented Apr 10, 2014 at 17:14

2 Answers 2

4

Change

level2_t level2[1] = {
                [0]={
                        [0]={"one",1},
                        [1]={"two",2},
                        [2]={"three",3}
                }
};

to

level2_t level2[1] = {
                [0].data = {
                        [0]={"one",1},
                        [1]={"two",2},
                        [2]={"three",3}
                }
};

Right now you're trying to initialize the structure as an array, while you need to initialize the data field.

As you asked in comments here's how to do the same thing without indexes:

level2_t level2[] = {
    {
        .data = {
            {"one", 1},
            {"two", 2},
            {"three", 3}
        }
    }
};
Sign up to request clarification or add additional context in comments.

1 Comment

by any chance do you know what would be the syntax without the square brackets [0],[1],[2] at all ?
0
level2_t level2 =
{
   {"one, 1},
   {"two, 2},
   {"three", 3}
};

2 Comments

This is a better way of initializing the entire data structure, as it is more portable.
yes, but it doesn't detail the nesting, also it misses fixed size of the array, what if I make a syntax error and initalize larger structure than I should?

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.