Archived

This topic is now archived and is closed to further replies.

linked list type definition problem

This topic is 5315 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Howdy, howdy, Can someone have a look at this structure definition please. I''ve got a few issues trying to use it as a node for a linked list. typedef struct BG_SPRITE16_TYP { int x,y; //pos on screen int height,width; //h,w on screen int source_i; //index of image in surface source i.e. type CELL16 *source; //pointer to source surface int state; //state of sprite - alive, dead, off-screen... struct BG_SPRITE16_TYP *up; //pointer to next sprite up linked list struct BG_SPRITE16_TYP *down; //pointer to next sprite down list } BG_SPRITE16, *BG_SPRITE16_PTR; Basically whenever I ran this code it had a lot of problems compiling concerning ''binary ''='''' operator. After a bit of fiddling, got it to run, to debug, and find out what i think is happening. Basically, *up, and *down are meant to be simple pointers to a similar structure, hence a linked list. However when it runs, the code defines *up and *down as entire structures (i.e. containg there own x,y values etc.), not simply pointers to this sorta structure. Any ideas? Bazz

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Not to sure about the typedef, what about

struct BG_SPRITE16_TYP {
int x,y;
int height,width;
int source_i;
CELL16 *source;
int state;
BG_SPRITE16_TYP *up;
BG_SPRITE16_TYP *down;
};

I wouldn''t recommend casting the pointer

Share this post


Link to post
Share on other sites
quote:
Original post by bazzamoore
Howdy, howdy,

Can someone have a look at this structure definition please. I''ve got a few issues trying to use it as a node for a linked list.

typedef struct BG_SPRITE16_TYP {
int x,y; //pos on screen
int height,width; //h,w on screen
int source_i; //index of image in surface
source i.e. type
CELL16 *source; //pointer to source surface
int state; //state of sprite - alive,
dead, off-screen...
struct BG_SPRITE16_TYP *up; //pointer to next sprite up linked list
struct BG_SPRITE16_TYP *down;
//pointer to next sprite down list

} BG_SPRITE16, *BG_SPRITE16_PTR;

Basically whenever I ran this code it had a lot of problems compiling concerning ''binary ''='' operator. After a bit of fiddling, got it to run, to debug, and find out what i think is happening.
Basically, *up, and *down are meant to be simple pointers to a similar structure, hence a linked list.
However when it runs, the code defines *up and *down as entire structures (i.e. containg there own x,y values etc.), not simply pointers to this sorta structure.

Any ideas?

Bazz


I''m a little curious as to why it''d be defining them as structures and not pointers. How do you know it''s doing this? Actually, I don''t think the compiler would let you do that, because the structure would take an infinite number of bytes to represent (because you could do something like anInstance.up.up.up.up.up...). I''d be more inclined to believe you''ve done something strange in your code. One guess is that you''ve done something strange in your assignment (binary = operator). Did you copy the address or the struct itself to the *up and *down variables? The latter should give you an error, but if you did a cast it would let you do it and, depending on how much memory was allocated (malloc can allocate more, but not less, memory than you request) it may actually let you do it.

Also, the suggestions about removing the "struct" keywords are clueless. In C++ it makes no difference, in C it wouldn''t compile without the "struct" keywords (in C struct/union/enum tags are all in the same namespace. This namespace is seperate from the typedef namespace.). A better suggestion would be to place a line saying "struct BG_SPRITE16_TYP;" before the actual struct definition, but that shouldn''t really make a difference (unless you''re using a really old C compiler).

Share this post


Link to post
Share on other sites
I think the problem is in you linked list functions. There don''t seem to be any problems with your struct definition. For example, run this code:

  
#include <iostream.h>

typedef struct BG_SPRITE16_TYP {
int x,y; //pos on screen

int height,width; //h,w on screen

int source_i; //index of image in surface source i.e. type

int state; //state of sprite - alive, dead, off-screen...

struct BG_SPRITE16_TYP *up; //pointer to next sprite up linked list

struct BG_SPRITE16_TYP *down; //pointer to next sprite down list


} BG_SPRITE16, *BG_SPRITE16_PTR;

void main(){

BG_SPRITE16 mystr1, mystr2;

mystr2.x = 14;
mystr1.down = &mystr2;

cout << mystr1.down->x << " <-- Should have printed ''14''\n";

mystr1.up->x = 5;
cout << "should seg fault and this line won''t print\n";

}


Runs pretty much as expected for me. Not exactly a bulletproof test, but it seems to indicate that up and down are pointers to structs, not structs themselves.

Share this post


Link to post
Share on other sites