Problems with basic SDL with C++: SDL_Rect

Hello,

I get an error(EXC_BAD_ACCESS) on the first declaration in the for loop in my code. Everything to do with SDL is properly included and initialized. I am sure the problem is because of a misunderstanding of pointers on my part, so please excuse my ignorance.

Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SDL_Rect *rect_map[69][100];

void initiate_rect_map()
{
    for(int i=0;i<68;i++)
    {
        for(int j=0;j<99;j++)
        {
            rect_map[i][j]->x=0+(10*j);//right here
            rect_map[i][j]->y=0+(10*i);
            rect_map[i][j]->w=10;
            rect_map[i][j]->h=10;
        }
    }
}


Thank you!
You have an array of pointer. The pointer needs to point to something, i.e. before it is allowed to access an element of the array you need new to create an instance of SDL_Rect:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SDL_Rect *rect_map[69][100];

void initiate_rect_map()
{
    for(int i=0;i<68;i++)
    {
        for(int j=0;j<99;j++)
        {
            rect_map[i][j]=new SDL_Rect{...}; // you might put the initialization in the constructor
            rect_map[i][j]->x=0+(10*j);//right here
            rect_map[i][j]->y=0+(10*i);
            rect_map[i][j]->w=10;
            rect_map[i][j]->h=10;
        }
    }
}
Is there a reason why you have a pointer? If not use an array of SDL_Rect directly.
Topic archived. No new replies allowed.