Sam
Sam

Reputation: 147

Initializing a 2D array inside a struct error

When I initialize this array inside my struct. I get the error message - syntax error : '{'. Unexpected token(s) preceding '{'; skipping apparent function body.

int array[8][2] = {{3,6},{3,10},{3,14},{8,4}, {8,8},{8,12},{8,16},{12,2}};

I'm not sure what is wrong as I copied the syntax from my textbook.

Declaration is typedef struct _array *Array;

Upvotes: 0

Views: 3885

Answers (1)

MByD
MByD

Reputation: 137362

You cannot initialize a variable inside a struct declaration, doesn't matter if an array or int. Yet, you can initialize the array in the struct initialization.

struct foo {
    int x;
    int array[8][2];
};

struct foo foovar = {1, {{3,6},{3,10},{3,14},{8,4}, {8,8},{8,12},{8,16},{12,2}}};

Upvotes: 4

Related Questions