gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > struct

#6194 - wiz - Mon May 19, 2003 12:44 pm

Hi, Im just wondering why this isnt working:

typedef struct TEST
{
u8 a;
u8 b;
u8 c;
u8 d;
};

I dont want to create the variable for this structure at the end of it as you would with:

typedef struct TEST
{
u8 a;
u8 b;
u8 c;
u8 d;
}VAR;

Can anyone help me?

Thanks!

#6198 - Torlus - Mon May 19, 2003 2:57 pm

Your second block of code :
Code:
typedef struct TEST { ... } VAR
doesn't mean that you create a variable. It means that you perform 2 operations :
- first declare a struct called TEST
- then define a type called VAR
That allows you declare variables like this : "VAR myTestVariable;"
If you only do "struct TEST { ... }" then you can declare a variable like this : "struct TEST myTestVariable;"
_________________
GBA,GC,NGPC,GP32,FPGA,DS stuff at http://torlus.com/

#6220 - wiz - Tue May 20, 2003 2:52 am

Thanks.

So I cant just simply state:

struct TEST
{
int a;
int b;
int c;
};

then in a function:

TEST t;

This is how I would do it in other compilers

but in devkitadv I get a parse error? :(

#6222 - niltsair - Tue May 20, 2003 3:09 am

It worked just fine for me, as it should. The error is somewhere else.

#6225 - Gordon - Tue May 20, 2003 4:22 am

I would do this!

typedef struct _TEST {
u8 a;
u8 b;
u8 c;
..
} TEST, *TESTPtr;

#6226 - Daedro - Tue May 20, 2003 4:24 am

Well.. I did this too.. I get errors if I so much as change the VAR.

Code:
typedef struct {
   const unsigned short w,h;
   const unsigned short *data;
}sLayer,*psLayer;

typedef struct {
   const aLayer *borderLayers;
   const unsigned char *tiledata;
   const unsigned short *pal;
   const unsigned int tileDataSize;
}sMap,*psMap;

#endif

extern const sMap borderMap;


I change the sMap's to aMap's and sLayer to aLayer, and I get syntax error at the last line. I also changed the sLayer, and sMap to aLayer and aMap in the other file:

Code:
const sLayer borderLayers[] = {
     {30, 20, borderMapLayer0},
};
const sMap borderMap = {
     borderLayers,
     borderTileData,
     borderPalette
, 11904};


.. looks exactly the same accept for the s is now a, does s stand for something.. if so can I change it to something else because I need this type of typedef twice for two different maps. I also tried changing Map and Layer to Map2 and Layer2 once.. didnt work either.

#6228 - wiz - Tue May 20, 2003 4:39 am

interesting!

I renamed my source file, from .c, to .cpp and compiled and it worked!?

why doesn't

struct NAME
{
int x;
int y;
};

work in C ?

I found that I had to do it this way:

typedef struct
{
int x;
int y;
}NAME;

I never knew this before? =/

I think the 1st method is much clearer, but that only seems available to C++ :(

#6230 - Daedro - Tue May 20, 2003 5:48 am

Very interesting, in .cpp ( which I am guessing is C++ coding ) and in .c the way you do a struct is different and has to be.. not too often you find those, dont they try to make all things in C able to do in C++, probably just couldnt do this in C++ becuase it conflicts with the more advanced version or just other coding.

I still have problems with mine though even in C.

#6232 - Daedro - Tue May 20, 2003 7:03 am

I'm sorry, I'm very dum. I forgot to change the

Code:
#ifndef LERNBG_H
#define LERNBG_H


at the top, a duh... thats a major conflict.

Thanks for that .c and .cpp information Wiz!