Advertisement

Arrays of Structures

Started by July 16, 2000 07:40 PM
4 comments, last by Chipcrap 24 years, 5 months ago
Greets All! I have a structure called Basic_Sprite that contains a bunch of integers, and a DDraw surface structure. At the beginning of my code I do this: Basic_Sprite S_Sprite[1]; That works fine. Then I do this: LoadGraphic(&S_Sprite[0],"graphic1.bmp"); That fine(LoadGraphic is a function to...well...load a graphic ). Then I do this: LoadGraphic(&S_Sprite[1],"graphic2.bmp"); This causes the program to lock up tight. I have no clue why. Any suggestions? Thanks In Advance!
Change it to:
Basic_Sprite S_Sprite[2];

This will allow you to use positions 0 and 1, whereas:
Basic_Sprite S_Sprite[1];
Allows you to use 0.

-Medgur
Advertisement
Because your array declaration only allocates space for one element. Use Basic_Sprite S_Sprite[2];

C/C++ begins its indexing at 0. When you declare an array of size 1 (as in your example s_Sprite[1]), you only declare one element which is indexed as the zero element, so when you try to reference s_Sprite[1] you''re accessing invalid memory. declare the array as:

Basic_Sprite S_Sprite[2];

This will give you 2 elements and your code should work.


Thanks All! Maybe I should try to get some more sleep before I code.


Thanks!
Hey! That''s my excuse!

-Medgur

This topic is closed to new replies.

Advertisement