SDL_Rect last array not being set?

Started by
1 comment, last by MrPickle 16 years, 2 months ago
I declare this as a global
SDL_Rect playerDown[2];
then I set the variables like
    playerDown[0].x = 6;
    playerDown[0].y = 1;
    playerDown[0].w = 17;
    playerDown[0].h = 26;
    
    playerDown[1].x = 41;
    playerDown[1].y = 1;
    playerDown[1].w = 17;
    playerDown[1].h = 26;
    
    playerDown[2].x = 76;
    playerDown[2].y = 1;
    playerDown[2].w = 17;
    playerDown[2].h = 26;
but it doesn't set playerDown[2], but the compiler doesn't complain that I'm trying to set it? If I make SDL_Rect playerDown[2], SDL_Rect playerDown[3] playerDown[2] then works but playerDown[3] doesn't, can anyone explain this please?
Here to learn :)
Advertisement
The array "SDL_Rect playerDown[2];" has two elements, indexable by 0 and 1.

Thus, there is no playerDown[2] rect, and accessing it is undefined behaviour (typically this will manifest itself as another, often unrelated, variable suddenly changing its value, or a segfault, but you cannot rely on this). If you want three elements, use "SDL_Rect playerDown[3];"
Yeah, I figured out what was wrong after like a hour and I was like "WTF, why isn't playerDown[2] being set"

Thanks.
Here to learn :)

This topic is closed to new replies.

Advertisement