-2

I have tried putting brackets everywhere I know to, but i keep getting the error "missing braces around initializer". This is an array of struct Point

Point pointArray[9][2]={1,1,-1,1,-1,-1,1,-1,1,0,0,1,-1,0,0,-1,0,5,1000,1};

I have put brackets around each set of points and 2 on each end, and it doesn't change anything

2 Answers2

2

Use this:

Point pointArray[9][2]={{1,1},{-1,1},{-1,-1},{1,-1},{1,0},{0,1},{-1,0},{0,-1},{0,5},{1000,1}};

The way you've done it, is like initialising a one-dimensial array array of 20 elements (Point pointArray[20]=...;).

However, that only half solves your problem, since you have 10 pairs in there, and you've specified 9. You would either have to delete an array entry, or change the definition to Point pointArray[10][2]=...;.

AntonH
  • 6,359
  • 2
  • 30
  • 40
  • That did not solve the problem, I got the same error – user3704616 Jun 08 '14 at 00:06
  • 1
    @user3704616 Do you get the same error? Or something else? Did you take into account last paragraph? – AntonH Jun 08 '14 at 00:06
  • logic is correct., but you have values for an array[10][2], as there are 10 row elements. – nikhil Jun 08 '14 at 00:07
  • 2
    @nikhil I know. I specified that exact problem, plus 2 possible solutions, in the last paragraph of my answer. – AntonH Jun 08 '14 at 00:09
  • @AntonH ... sorry, i didnt read your answer completely.. my bad. i just wanned to help get it straight – nikhil Jun 08 '14 at 00:11
  • @nikhil No problem. Happens to me all the time :) – AntonH Jun 08 '14 at 00:12
  • You can initialize multidimentional arrays in only one pair of brackets like that http://stackoverflow.com/questions/18157251/multidimensional-array-initialization-in-c – phuclv Jun 08 '14 at 03:15
2

If you have a structure type like:

typedef struct Point
{
    int x;
    int y;
} Point;

then the fully braced version of the initializer should be:

Point pointArray[9][2] =
{
    { {  1,  1 }, { -1,  1 } },
    { { -1, -1 }, {  1, -1 } },
    { {  1,  0 }, {  0,  1 } },
    { { -1,  0 }, {  0, -1 } },
    { {  0,  5 }, { 1000, 1 } },
    // 4 uninitialized rows in the array - populated with zeros
};

The innermost sets of braces surround the structures; the middle sets of braces surround pairs of structures, corresponding to entries in the [2] dimension of the array. The outermost braces surround 5 of the possible 9 initializers for the first dimension [9] of the array.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278