GP32 Array Subscript Warning


pea

developer
Joined
Oct 3, 2004
Messages
1,089
Age
46
Location
New Zealand
Website
www.projectitis.com
Hi all,

I am compiling code that gets the following warning:
Code:
warning: array subscript has type `char'
in relation to the following code:
Code:
unsigned short level;
char a=1;

level = &gp_alphaLUT[a];

And, yes, it does have an array subscript of type char, but who cares?? What does the compiler care?

On a related note - i have always learned that you should choose the datatype that best matches the range of data that you are expecting (e.g. unsigned char for anything that won't go over 255). But is it better to just use 'int' anyway because it is the native 32 bit variable? Will it be more efficient using an int over a char (for example, array subscripts, additions and other math, shifts etc)?
 
That's due to gcc defaulting to a signed char just put unsigned infront of the char and the warning should disappear ;)

But if someone has a better suggestion you should listen to them instead :p
 
On a 32-bit processor, assuming you have memory to hold the variables, it's always best to use a 32-bit variable, and then the compiler doesn't waste time trying to mask the variable down to whatever size you have stated after every maths operation like assignment, addition, etc. Sometimes the compiler will be smart enough to mask out some of the masking, but if you have the memory, it's easier to just use the size of the processor data registers.

Signed char's only go upto 127 and then go negative, so an array with more than 127 can quickly buffer underrun, which is why the compiler is warning you.
 
Bah, woogal being lazy again ;)

'char' is signed most of the time, except for broken compilers. 'unsigned char' is 0-255, char is -127->128. Arrays can't be -ve.. have to be unsigned types.

IMHO, it should be an error and not a warning :)

jeff
 
I've used a negative subscript a few times - for example, if you have an array of, say, 1024 bytes, and a char * pointing into the middle of that, you can quite happily use negative offsets into that and get known results.

Then again, perhaps I'm just not normal ;)
 
Back
Top