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++ > Initializing array of structs?

#4712 - Alunze - Mon Apr 07, 2003 9:22 pm

I know this question is kind of lame, but it's something which would greatly help to my code's clarity and organization. How can I initialize an array of structs in the array's declaration?.

Thanks in *advance* >_6

#4716 - tepples - Mon Apr 07, 2003 9:58 pm

Alunze wrote:
How can I initialize an array of structs in the array's declaration?

The same way you initialize an array of arrays:
Code:
typedef struct Player
{
  int foo, bar, baz;
} Player;

Player p[2] =
{
  { 1, 2, 3 },
  { 4, 5, 6 }
};

And add "const" in front of the data type if you want it to go into ROM.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#4722 - Alunze - Mon Apr 07, 2003 10:57 pm

Aha, so that's how it's done. I was having trouble figuring out the syntax, thanks again :).

#4730 - darkcloud - Tue Apr 08, 2003 1:16 am

Actually, I've been wondering if its possible to initialize and entire array with a single value. What I mean is that say I declare an int array of 100, is there a way to set all the values of that array without having to manually set them.
Something like

int array[100] = 0; and that would set all values to 0.

Anything like that?
_________________
Maybe in order to understand mankind, we have to look at the word itself: "Mankind". Basically, it's made up of two separate words - "mank" and "ind". What do these words mean ? It's a mystery, and that's why so is mankind.

#4732 - tepples - Tue Apr 08, 2003 5:18 am

darkcloud wrote:
I've been wondering if its possible to initialize and entire array with a single value. What I mean is that say I declare an int array of 100, is there a way to set all the values of that array without having to manually set them.

It's possible, but only if you're initializing all the values to 0 and not to any other value. The key is that any omitted values in the array are set to 0:

int array[100] = {6, 8, 9};

which initializes the array to {6, 8, 9, 0, 0, 0, ..., 0}. However, this may take up as much space on the cart (or worse, in the multiboot binary) as if you had written out all the 0's because of limitations in the linker's architecture.

Another solution that works without inflating the binary is to rely on a detail of the C language specification: all statically allocated variables (global variables, static variables in module scope, and static variables in function scope) are guaranteed to contain 0 at the start of the program.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#4735 - Daikath - Tue Apr 08, 2003 9:28 am

wouldt this work?

Code:

u16 loop;
for (loop = 0;loop <100; loop++)
{
array[loop] = 0;
}


Becuase I get the idea array[100] = 0; would only make the variable in place 100 of the array zero.
_________________
?There are no stupid questions but there are a LOT of inquisitive idiots.?

#4790 - CcSoccer881 - Wed Apr 09, 2003 9:00 pm

I believe that you are correct..
_________________
www.rsstudios.net

#4792 - darkcloud - Wed Apr 09, 2003 9:18 pm

Yes that would work, but what I was asking for is a way to do that when I declare the array.

Thanks tepples for explaining it.
_________________
Maybe in order to understand mankind, we have to look at the word itself: "Mankind". Basically, it's made up of two separate words - "mank" and "ind". What do these words mean ? It's a mystery, and that's why so is mankind.

#4804 - Sweex - Thu Apr 10, 2003 9:28 am

I think your best shot to initialize an array on the same line of the declaration is by using memset.

Code:

int array[100] = memset(array, 0, sizeof(array));


If you're doing C++, you might be able to do some trickery with templates and end up with something like this...

Code:

tArray<int, 100> array;

#5069 - nintendodev - Sat Apr 19, 2003 10:04 am

Daikath wrote:
wouldt this work?

Code:

u16 loop;
for (loop = 0;loop <100; loop++)
{
array[loop] = 0;
}


Becuase I get the idea array[100] = 0; would only make the variable in place 100 of the array zero.


Sir, go faster:

Code:

u16 loop;
VAR * ptrArr = array + 99;

for( loop = 100; loop' --loop _{
 *ptrArr-- = 0;
}


Do a tcc or armcc -S -fs on this routine. Comparing 0 is shorter op than occupying a register ^_^*