SDL rectangle pointer error [Archive] - C Board

PDA

View Full Version : SDL rectangle pointer error


cozman
08-19-2001, 09:06 PM
This code compiles fine but the program crashes.


//global rectangles
SDL_Rect *player1, *player2, *credits, *exitt;

//function to set coords of rectangles
void SetRects(void)
{
cout << "in setrects" << endl;
player1->x = 1;
cout << "set player1.x" << endl;
player1->y = 1;
player1->w = 94;
player1->h = 26;
...
}



when i run this program (yes there is a lot more to it) i see the screen switch modes, and everything that comes before my call to the function SetRects(), but inside setrects the program crashes, i put in the cout statments to find the error and sure enough, i see the words "in setrects" but i never see the "set player1.x" part. What am I doing wrong in this code?

Nick
08-19-2001, 10:43 PM
The trouble your having is your dereferencing a stray pointer.
Probably best to do something like

struct Player {
int x, y;
};

typedef struct Player Player;

void init_player(Player* p)
{
p->x = 1;
p->y = 1;
}

cozman
08-20-2001, 01:43 PM
yeah, thanks, can't belive i didn't see that :p