This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

array index type of signed int

Hi list,

i'm working with c51v750a. My problem:

int array[ 0] _at_ 0x7600; //xdata
unsigned char index = 0; //xdata

printf("%d", &array[ index]);
printf("%d", &array[(unsigned char)(index - 1)]); //what is wrong?

// Result:
// 30208 --> 0x7600 (correct)
// 30206 --> 0x75FE == array[ -1] incorrect, // expected array[255] @ 0x77FE

I've read in Compiler's changelog [v7.50] that there was a fix for array with signed int index.

I don't need a workaround, but this seems like a compiler-bug. Or is my "logic" incorrect?

Thanks in advance,
Lars

Parents
  • I want to create a circular buffer with size of 256 (0-255)integers. The output is LIFO. In my case, the index-counter is decremented. The type of the index counter is unsigned char (0-255). There is no problem when i first decrement the index and then read/write the array. But when i put the result of the calculation (index - 1)

    array[(unsigned char)( index - 1)] //THIS IS AN EXAMPLE
    
    the result of the index-counter is -1, for index = 0. The result without typecast is correct ( -1, int), but when i put a typecast before, it has to be 255!! I know how to avoid this problem, but in my opinion this is a bug in the compiler.

Reply
  • I want to create a circular buffer with size of 256 (0-255)integers. The output is LIFO. In my case, the index-counter is decremented. The type of the index counter is unsigned char (0-255). There is no problem when i first decrement the index and then read/write the array. But when i put the result of the calculation (index - 1)

    array[(unsigned char)( index - 1)] //THIS IS AN EXAMPLE
    
    the result of the index-counter is -1, for index = 0. The result without typecast is correct ( -1, int), but when i put a typecast before, it has to be 255!! I know how to avoid this problem, but in my opinion this is a bug in the compiler.

Children