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++ > Array and Pointers question

#6781 - einhaender - Mon Jun 02, 2003 6:56 pm

Hi, iam new to C/C++ so maybe theres someone to help me on this one.
I have multiple header files holding u16 arrays, like:

const u16 tile_1_1Data[] = { ... }
const u16 tile_2_1Data[] = { ... }

now in my main file I need to iterate through all these array but I dont wanna write the for loop over and over again. so somehow i need to
assign to a new Array the actual Array that I wanna iterate. I thought of something like:

u16 new_array[1]; //
for(int i = 0; ...... ) {
switch(i) {
case 1: new_array[0] = tile_1_1Data;
break;
etc. etc.
}

for(int n = 0; ......) {
u16 test = new_array[0][n]; // this should be tile_1_1Data if i==0
}
}

The idea is to put the array to iterate into new_array[0] and get a multidimensional array. It doesnt work however?
Iam not too familar with pointers but iam sure theres a much better
solution.
Also is there a way in a for loop to limit iterations to the array's size?
This question may sound stupid but all sample code snippets have the
number of iterations hardcoded, i.e for(int i=0; i < 128 ....

#6782 - Touchstone - Mon Jun 02, 2003 7:02 pm

What you need is an array of pointers. Arrays and pointers are basically the same except for with pointers you have no way of knowing the size.

const uint16* new_array[1];
new_array[0] = tile_1_1Data;

To get the number of entries in the array you can do:
int nEntries = sizeof(tile_1_1Data) / sizeof(tile_1_1Data[0]);

Check out the sizeof-operator or whatever the heck it is. :)
_________________
You can't beat our meat

#6885 - einhaender - Wed Jun 04, 2003 5:53 am

Thanks!