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 aggregate initialization

#23191 - mr_schmoe - Wed Jul 07, 2004 2:19 am

I need to do something like this:

Code:

struct
{
   u8 size, shape;
   u8 numcolors;
   u8 pal_no;
   u16 data[];
} sprite_data_type;

const sprite_data_type player = {SIZE_32, TALL, 0, 0, {0x0000, 0x0001, ..., 0x0002} };


The funny thing is the above code doesn't work. I get an error something like "error: excess elements in aggregate initializer". I find out that if I define the number of elements in the array, u16 data[256], it works just fine. But I can't do that because not every sprite will have the same amount of data. So, any suggestions?
_________________
How many boards could the Mongols hoard, if the Mongol Hored got bored

#23193 - poslundc - Wed Jul 07, 2004 2:49 am

You cannot directly "embed" the data as you do when you declare the size of the array, because the internal representation when you don't declare the size is actually a pointer to an array not contained within your struct.

One method to make it work in the way you've declared it would be to define the array of data separate from the struct, eg:

Code:
static const u16 playerData[] = {0x0000, 0x0001, ..., 0x0002};

const sprite_data_type player = {SIZE_32, TALL, 0, 0, playerData};


Hope this helps,

Dan.

#23195 - DiscoStew - Wed Jul 07, 2004 3:47 am

Wouldn't there need to be a change on the array u16 data[] to be a pointer like this...

u16 *data;

That way the playerData array can be used.
_________________
DS - It's all about DiscoStew

#23197 - mr_schmoe - Wed Jul 07, 2004 4:15 am

Ya, that helps. I was thinking of doing it that way too. Thanks again.
_________________
How many boards could the Mongols hoard, if the Mongol Hored got bored

#23204 - poslundc - Wed Jul 07, 2004 1:39 pm

DiscoStew wrote:
Wouldn't there need to be a change on the array u16 data[] to be a pointer like this...

u16 *data;

That way the playerData array can be used.


Hm... I just tested this and it appears to be the case, although I would've thought the two notations would have been interchangeable.

Dan.

#23268 - sajiimori - Fri Jul 09, 2004 1:24 am

Structs with arbitrarily-sized arrays as their last elements are not meant to be instantiated directly. The idea is to cast to that struct type, and it's a bit of a hack that got official support in C99.

It doesn't add a pointer to the struct. It just says that there could be any number of array elements, and that those elements will be part of the struct. This is confusing because using the same notation in an argument list declares a pointer.

So in summary, there are 3 places where you can use empty brackets: initializing regular arrays, declaring pointers in argument lists, and when using the undefined-struct-size hack.

#23270 - MumblyJoe - Fri Jul 09, 2004 2:29 am

There is another way if you like it (and if C++ doesn't scare you)...

Code:
template<unsigned int L>
class sprite_data
{
   public:
   unsigned char size, shape;
   unsigned char numcolors;
   unsigned char pal_no;
   unsigned short data[L];
};

sprite_data<5> player = {32,3,0,0,{0x0000,0x0001,0x0002,0x0003,0x0004}};



It's a bit of a cheat but it's legal. Only downside is that the template argument cannot be dynamic, it has to be constant.
_________________
www.hungrydeveloper.com
Version 2.0 now up - guaranteed at least 100% more pleasing!

#23308 - mr_schmoe - Fri Jul 09, 2004 6:22 pm

No, C++ doesn't scare me. In fact, I'm use it quite extensively when programming my games. However, there are still parts of C++ I'm still learning. For example, I'm not sure what template does
Quote:
template<unsigned int L>

and what those less-than greater-than signs are suppose to do
Quote:
sprite_data<5> player = {32,3,0,0,{0x0000,0x0001,0x0002,0x0003,0x0004}};

_________________
How many boards could the Mongols hoard, if the Mongol Hored got bored

#23344 - sajiimori - Sat Jul 10, 2004 5:33 am

There are lots of great C++ books out there -- my favorite is Bruce Eckel's "Thinking in C++". It really explains the point of every feature that was added to C.

#23350 - MumblyJoe - Sat Jul 10, 2004 6:49 am

I would also recomend Bruce Eckel's book, and also anything by Scott Myers.

Anyway, the simple explanation is that a template allows you to quickly create new classes from a template, not much more to it than that. Template parameters are passed in the <> fasion that I showed before. By doing this you can say anything from what constant number you want the class to use, to what class you want it to use internally. Actually, I think a book would be a good place to start...
_________________
www.hungrydeveloper.com
Version 2.0 now up - guaranteed at least 100% more pleasing!