GP32 Array Definition


mATkEUpON

Certified Guru
Joined
Sep 30, 2003
Messages
276
Is there any way to tell a c compiler an array will ony be used from the 500th value ???

Thanks in advance.
 
I'm assuming you mean for the purpose of a lookuptable? I haven't heard of any way of doing that. You may just need to #define an offset and just add that to each number.

ie.

#define LU_OFFSET -500

int test = lookup_table[i + LUOFFSET]

Something like that.
 
No, only a few languages (VB, Pascal and some BASICs) support that kind of range. C, as a low-level language, doesn't. My suggestion is simply to define it as normal then take 500 from all lookups. From:

int stuff[500..600]; // pseudo-pascal, you can't really do this

printf( "%d", stuff[501] );

Instead:

int stuff[500];

printf( "%d", stuff[501 - 500] );

Which gets turned into stuff[1] at compile-time.

Of course, if you can skip the - 500s altogether, your program will probably be faster.
 
hi,

what exactly are you trying to do? If what you want do is access location 500 as if it was 0, could you just add a constant value of 500 when you use the array,

i.e.


#define OFFSET 500
.
.
.
array[OFFSET+3] /*for location 503*/


hope that helps, but i'm not really sure what you are asking, or why you would want to only use the values after the 500th...

sam
 
Yes I could add an offset to each access, but I'd like to avoid that, as it takes one more addition, and I read data from that array very often in my loops. I guess Rico answered to my question..
 
It will only compile like that if you are using constants. If you are using the lookup with variables, that addition/subtraction will not be compiled off.

Maybe there is a way. If you get a pointer to the start of the array, and then take 500 off that.. Then access that pointer like an array. As long as you don't try and access any of the entries below 500, it shouldn't throw up an error.

ie.

int main(int argc, char *argv[])
{
int LUT_Data[500]; //create a look up table with 500 entries
int *p_LUT = &LUT_Data; //create pointer to the look up table
p_LUT -= 500; //offset your pointer by -500

int i;

for(i=500; i<1000; i++){ //fill the look up table with values
p_LUT = rand() % 500;
}

for(i=500; i<1000; i++){ //read values from the the look up table
printf("%d ",p_LUT);
}

return 0;
}

That is your solution.. :)

Edit: I think I deserve the status of GP32 Guru for that ;)
 
Thanks, I think I'll use that, it will save some space in memory !!

My array is an array of a structure, so I think I should use:
p_LUT -= 500 * sizeof(the_sructure);

Thanks !
 
Daz_Genetic, I knew that compilation would not speed it up when using variables, that's why I suggested it would be better to modify loops and such to get rid of the 500 start point altogether.

You need only do simple stuff like change the loops to 0...499 instead of 500...999, substract 500 from a variable before doing all your lookups, etc.

As mATkEUpON hinted, you need to do that, and in DG's original example you would need to subtract 1000 as integers are two bytes long.

Other than that, I think DG's method was both intuitive and arousing. Therefore I would agree that he should get GP Guru :)
 
I'm really not sure you need to take away 1000 in that example. I've tested it and it works fine. I think since the pointer is of type int, it moves the sizeof an int with each increment.

So if your pointer is defined as the type if your struct, it should move the sizeof the struct at a time.

ie.

int main(int argc, char *argv[])
{
typedef struct mystruct{
int test;
int test2;
}mystruct;

mystruct LUT_Data[500]; //create a look up table with 500 entries
mystruct *p_LUT = &LUT_Data; //create pointer to the look up table
p_LUT -= 500; //offset your pointer by -500

int i;

for(i=500; i<1000; i++){ //fill the look up table with values
p_LUT.test = i;
p_LUT.test2 = 1000 - i;
}

for(i=500; i<1000; i++){ //read values from the the look up table
printf("%d %d :",p_LUT.test, p_LUT.test2);
}

return 0;
}

So what I'm saying is.. You shouldn't need to use the sizeof(), you should be able to just add or subtract however many entries you want to offset your table by.

Does that make sense?
 
Daz_Genetic posted on Apr 2 2004 at 11:09 AM said:
I'm really not sure you need to take away 1000 in that example. I've tested it and it works fine. I think since the pointer is of type int, it moves the sizeof an int with each increment.
Yes, you shouldn't need to do -1000.
Even if you did, it would be 2000 since ints are 4 bytes on a Gp32
 
Last edited by a moderator:
Back
Top