Path: chuka.playstation.co.uk!scea!sumner From: sumner@austin.metrowerks.com (Joel Sumner) Newsgroups: scee.yaroze.programming.codewarrior,scea.yaroze.programming.codewarrior,scea.yaroze.freetalk,scee.yaroze.freetalk Subject: Pointer usage (Was Re: Now I'm getting REALLY mad!) Followup-To: scee.yaroze.programming.codewarrior Date: Fri, 19 Dec 1997 12:01:20 -0600 Organization: Metrowerks Lines: 94 Message-ID: References: <34902D9A.A58C5281@tin.it> <01bd094f$aa411420$6c0b0a0a@newcastle.twowaytv.co.uk> <3496FC87.B1AD07DF@tin.it> <01bd0a92$e4e72b20$LocalHost@Angela1.intelligent-group.com> <349A9CA1.7E336240@tin.it> NNTP-Posting-Host: mtwks134.metrowerks.com Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Newsreader: Yet Another NewsWatcher 2.4.0 Xref: chuka.playstation.co.uk scee.yaroze.programming.codewarrior:120 scea.yaroze.programming.codewarrior:262 scea.yaroze.freetalk:194 Hi Guilio, I'm glad you posted your source for this. I think I know what's going on. This may be helpful for others having this problem. In article <349A9CA1.7E336240@tin.it>, Giulio wrote: >typedef struct SpriteType >{ > GsSPRITE *SprBody ; // Body of the sprite using library > int SprOld_X,SprOld_Y ; // Old sprite's position > u_char AnimClock ; // Animation clock > u_char AnimSpeed ; // Animation speed > u_char MotionClock ; // Motion clock > u_char MotionSpeed ; // Motion speed > u_char CurrentFrame ; // Current frame being displayed > u_char NumFrames ; // Total number of frames > u_char State ; // State of the sprite (Dead, Alive...) > u_char Hits ; // How many hits the sprite has got > u_char Doing ; // What the sprite is doing > u_char Type ; // What kind of sprite is > // being processed > // Other sprite related variables.... >} Sprite, *SpritePtr ; > >Sprite PlayerOne [1] ; // Ugly struct of player one that > // I must use to make my program run > // using PlayerOne as a pointer > >// SpritePtr PlayerOne ; // Right pointer def that it's not usable >// Sprite *PlayerOne ; // Eh,eh too easy to be able to use this I think I see your original problem. Defining a *pointer* to a type, does not allocate space for the type itself. When you do the following SpritePtr PlayerOne; You have allocated space for a POINTER to a structure. Not the structure itself. You can't use this pointer until you tell it to point to something (a SpriteType structure). So how do you make space for a SpriteType structure? There are two ways. 1) Have the compiler generate space for it. The following code sequence will do this // allocate space for two SpriteType objects Sprite gSprite[2]; // set the PlayerOne to point to an area in memory that holds a Sprite object SpritePtr PlayerOne=&gSprite[0]; // set the PlayerTwo pointer to point to an area in memory that holds a Sprite object SpritePtr PlayerTwo=&gSprite[1]; 2) Allocate it at runtime. The above sequence works because you know at compile-time how many players you are going to support (max two). However, maybe you only want to allocate space for the second player if they have selected a multi-player game. You can allocate memory at runtime using the "malloc()" library function. // Allocate space for the POINTERs to the SPRITE objects, not the SPRITE objects themselves. SpritePtr PlayerOne; SpritePtr PlayerTwo; PlayerOne=(SpritePtr)malloc(sizeof(SPRITE)); // allocate memory for PlayerOne if (numPlayers==2) PlayerTwo=(SpritePtr)malloc(sizeof(SPRITE)); // allocate memory for PlayerTwo else PlayerTwo=NULL; // always initialize pointers, even if you are not using them. It helps catch runtime errors One thing you need to do if you malloc() memory is free() it when you are done. Thus, at the end of your game, you should // release memory from the heap. free(PlayerOne); free(PlayerTwo); (note: in order to use malloc(), you must call Init_Heap(). See the _psstart.c file for information on doing this) I hope this helped. If you have any other code questions, please don't hesitate to post examples. There are a lot of people here who can help. (followups to scee.yaroze.programming.codewarrior) -- PlayStation Development Tools Dude Metrowerks