GP32 Struct Array In Struct


mATkEUpON

Certified Guru
Joined
Sep 30, 2003
Messages
276
Hi all!

I have these structs declared:

Code:
typedef struct {
     int length;
     unsigned short *sound;
} s_sound;

typedef struct {
     short poids, nsauts;
     char name[32];
     short idcoups, idphoto, idicone;
     //
     s_sound son[32];
     short nsons;
     char pakname[16];
     short idpak;
     short required;
} s_defperso;

And this function:
Code:
int loadwave(const char *name, s_sound *son);

But I can't do that:
loadwave(tmpbyte, (s_sound *)defperso[id].son);

Gcc returns this:
error: cannot convert to a pointer type

I feel very stupid right now, must be missing something quite obvious. Thanks for any help :)
 
I'm not an expert at C, I'm still learning myself. But could it be because

loadwave(tmpbyte, (s_sound *)defperso[id].son);

should be

loadwave(tmpbyte, (s_sound *)&defperso[id].son);
 
Yep, true !!

I don't really get when you have to put that & operator, but I guess I should try it before posting... :unsure:
 
Yep, you need the &.

Remember that essentially by using [] to "dereference" the array, you're bringing out the real object contained in the array, which in this case is a struct.

Consider..

int x [ 10 ]; // x is an array of 10 int, from 0 through 9

If you refer to "x", you're referring to an int[] or int* object.

But if you refer to x[4] you're referring to an _int_ right? ie: You stuck int's in there, and ask for int number 4, so you get an int back.

So if you stick s_sound into an array, and then ask for element number 5, its a s_sound. Not a pointer to one.

jeff
 
skeezix posted on Mar 7 2005 at 09:42 PM said:
So if you stick s_sound into an array, and then ask for element number 5, its a s_sound. Not a pointer to one.

Makes sense...

I guess gcc can give bad habits being too much convenient... Because I could do that:
Code:
loadwave("cpasse.wav", sound[son_cpasse]);

So I'll use & from now.. and knowing why :)

Thanks
 
Last edited by a moderator:
I'm also not much of a C wizard, but would loadwave(tmpbyte, &defperso[id].son); work? i.e. without the cast.
 
Yep, it works too !

I also wonder if there is a less permissive mode for the gcc parser... Sharing code with people compiling under ADS gives extra work for the one with ADS...
 
Theres a number of modes for gcc .. gcc has a lot of extra extension sin it, which sometiems can be convenient and then bite you in the ass later. So they have -pedantic and strict moces and such.

But just use -Wall so you find all your typos; fix almost every warning..

jeff
 
Back
Top