-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
  • 2
    It would be helpful if you could edit the question and supply the definition of 'Point'. Commented Jun 8, 2014 at 0:00
  • Could you also supply which compiler you're using? Commented Jun 8, 2014 at 9:28

2 Answers 2

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]=...;.

Sign up to request clarification or add additional context in comments.

7 Comments

That did not solve the problem, I got the same error
@user3704616 Do you get the same error? Or something else? Did you take into account last paragraph?
logic is correct., but you have values for an array[10][2], as there are 10 row elements.
@nikhil I know. I specified that exact problem, plus 2 possible solutions, in the last paragraph of my answer.
@AntonH ... sorry, i didnt read your answer completely.. my bad. i just wanned to help get it straight
|
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.