0

i am trying to create a labelled 2d array of GRects called brick[i][j]. implementing the code below i am getting an error:

breakout.c:112:23: error: variable-sized object may not be initialized ...brick[i][j] = newGRect(i*(40/400) + (2/400), j*(60/600) + (6/600), 36*WIDTH/400, 36*HEIGHT/600); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

i used the variables WIDTH and HEIGHT when initialising the paddle so i don't know why it has a problem with this method. i guess it might have something to do with calling i and j in the way i have? but i can't see how...

void initBricks(GWindow window)
{
    for (int j; j < COLS; j++) 
        {
            for (int i; i < ROWS; i++)
            {
                GRect brick[i][j] = newGRect(i*(40/400) + (2/400), j*(60/600) + (6/600), 36*WIDTH/400, 36*HEIGHT/600);
            }
        }
}
2

You don't really need to have an array to store bricks. Once you create and add a brick to the window, it stays there until you remove it using the function named removeGWindow() from the SPL library passing to it a reference to that brick after you get that reference returned by detectCollisio().

I guess the error you're getting is because you're declaring the array and specifying its size with a variable that's not guaranteed to be initialized though.

5
  • i was thinking that i could just place a brick in the window and then remove it but i couldn't see how i was going to remove the brick that was collided with if the program didn't know which brick had been hit by having each one labelled. Jun 15 '14 at 23:08
  • sorry tried to edit this but waited too long... Jun 15 '14 at 23:15
  • i used an array because couldn't see how i was going to remove the brick that was collided with if the program didn't know which brick had been hit. i figured i would label each initialised brick as part of a 2d array of bricks so that if brick[i][j] is hit, remove brick[i][j]). Jun 15 '14 at 23:16
  • The program knows whether a GObject (e.g., a GRect) is at x, y by calling a function from the SPL library named getGObjectAt() that returns that GObject if it exist at x, y.
    – kzidane
    Jun 15 '14 at 23:21
  • aha i didn't realise that. thankyou. Jun 16 '14 at 0:09
0

The problem according to me is that you have tried to declare and initialize the array in the inner loop itself. Try declaring the labelled array outside as a global variable or declare the array outside the for loops.

I hope this helps.

0

I used an array like you're showing. when you declare the variable, you have to tell it what the i an j are so it knows how much memory to allocate (hope i'm saying that right). so, GRect Brick[5][10]; or something like that before you start the loop, the just brick[i][j]= when you're in the loop.

1
  • GRect brick[x][y]; where x is the number of rows and y is the number of columns and they're both integers declares a multi-dimensional array of GRects. After you declare the array you may initialize its elements (the GRects) sequentially by looping through all the columns within each row starting with the first row and the first column (x = 0 and y = 0) as in board[0][0] = newGRect(0, 0, 20, 10);. I believe what you meant to say is that you have to declare a variable before you use it.
    – kzidane
    Jun 23 '14 at 10:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .