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++ > indexing arrays names

#45810 - kouky - Wed Jun 15, 2005 10:35 am

is there a way to simplify this :

jingle01_Map, jingle02_Map.... are arrays.


Code:
switch (n)
{
case 1 : bg_vido = ham_InitMapFragment((void*)jingle01_Map,30,20,0,0,30,20,0);break;
case 2 : bg_vido = ham_InitMapFragment((void*)jingle02_Map,30,20,0,0,30,20,0);break;
case 3 : bg_vido = ham_InitMapFragment((void*)jingle03_Map,30,20,0,0,30,20,0);break;
case 4 : bg_vido = ham_InitMapFragment((void*)jingle04_Map,30,20,0,0,30,20,0);break;
case 5 : bg_vido = ham_InitMapFragment((void*)jingle05_Map,30,20,0,0,30,20,0);break;
case 6 : bg_vido = ham_InitMapFragment((void*)jingle06_Map,30,20,0,0,30,20,0);break;
}


by using something which would look like :
ham_InitMapFragment((void*)jingle (n) _Map,30,20,0,0,30,20,0);break;
???
It would be very helpfull !

#45819 - strager - Wed Jun 15, 2005 2:51 pm

Code:

const void *jingle_Maps[] = {
jingle01_Map,
jingle02_Map,
jingle03_Map,
jingle04_Map,
jingle05_Map,
jingle06_Map
};

...

if(n > 0 && n <= 6)
{
    bg_vido = ham_InitMapFragment((void*)jingle_Maps[n],30,20,0,0,30,20,0);
};


Much easier :)

#45824 - kouky - Wed Jun 15, 2005 3:44 pm

arg ! thank you, but it's impossible !
jingle01 is allready an array !

and i can't make a 2d array "jingle" because the gba will never be able to load it !

#45826 - strager - Wed Jun 15, 2005 4:19 pm

kouky wrote:
arg ! thank you, but it's impossible !
jingle01 is allready an array !

and i can't make a 2d array "jingle" because the gba will never be able to load it !


Say what? That array is an array of pointers to your array. It is not the actual array itself. Make sure you have your arrays defined with the const keyword, or you will be waisting memory.

#45835 - sajiimori - Wed Jun 15, 2005 5:48 pm

Also be careful of the off-by-one bug in the example. Index by n-1 instead of n. (Better yet, let n be 0-5.)

#45844 - strager - Wed Jun 15, 2005 7:29 pm

sajiimori wrote:
Also be careful of the off-by-one bug in the example. Index by n-1 instead of n. (Better yet, let n be 0-5.)


To kouky: This means change my code to:
Code:
if(n >= 0 && n < 6)
{
    bg_vido = ham_InitMapFragment((void*)jingle_Maps[n],30,20,0,0,30,20,0);
};


To sajimori: Thanks for pointing that out!