Here is your code below with debug output added. In main cycle it is running a bit out of array (on purpose) over the memory that it occupies and it works fine so far (it might be still platform incompatible though). I am pretty sure your foo()
is OK and compiler is too, but you probably wrongly call it. Check all addresses and values with debug output and check ranges.
void foo(void* data, unsigned int length)
{
unsigned int i,j;
unsigned int block;
for(i = 0, j = 0; i < length; i+= sizeof(unsigned int ),j++)
{
printf("Debug 0\r\n");
printf("i: 0x%d\r\n", i);
printf("j: 0x%d\r\n", j );
#if 0//working code
block = ((unsigned char*)data)[i + 3];
block <<= 8;
block |= ((unsigned char*)data)[i + 2];
block <<= 8;
block |= ((unsigned char*)data)[i + 1];
block <<= 8;
block |= ((unsigned char*)data)[i + 0];
#else//not working code
block = ((unsigned int *)data)[j];
#endif
printf("data: 0x%x\r\n", data );
printf( " ((unsigned int )data +j) = 0x%x\r\n", ((unsigned int )data +j) );
printf( " ((unsigned int *)data)[j] = 0x%x\r\n", ((unsigned int *)data)[j]);
printf("block: 0x%x\r\n", block );
printf("Debug 1\r\n");
printf("\r\n");
printf("\r\n");
}
}
int main(void)
{
unsigned int array[10] = {0xFE,0xED,0xBA,0xBE,0xDE,0xAD,0xBE,0xEF,0xCA,0xFE};
printf ("array: 0x%x\r\n",array);
printf ("array[1]:%x\r\n",array[1]);
foo((void*) array, 50);
}
its output:
array: 0x1d0fdde0 array[1]:ed Debug 0 i: 0x0 j: 0x0 data: 0x1d0fdde0
((unsigned int )data +j) = 0x1d0fdde0 ((unsigned int *)data)[j] =
0xfe block: 0xfe Debug 1
Debug 0 i: 0x4 j: 0x1 data: 0x1d0fdde0 ((unsigned int )data +j) =
0x1d0fdde1 ((unsigned int *)data)[j] = 0xed block: 0xed Debug 1
Debug 0 i: 0x8 j: 0x2 data: 0x1d0fdde0 ((unsigned int )data +j) =
0x1d0fdde2 ((unsigned int *)data)[j] = 0xba block: 0xba Debug 1
Debug 0 i: 0x12 j: 0x3 data: 0x1d0fdde0 ((unsigned int )data +j) =
0x1d0fdde3 ((unsigned int *)data)[j] = 0xbe block: 0xbe Debug 1
Debug 0 i: 0x16 j: 0x4 data: 0x1d0fdde0 ((unsigned int )data +j) =
0x1d0fdde4 ((unsigned int *)data)[j] = 0xde block: 0xde Debug 1
Debug 0 i: 0x20 j: 0x5 data: 0x1d0fdde0 ((unsigned int )data +j) =
0x1d0fdde5 ((unsigned int *)data)[j] = 0xad block: 0xad Debug 1
Debug 0 i: 0x24 j: 0x6 data: 0x1d0fdde0 ((unsigned int )data +j) =
0x1d0fdde6 ((unsigned int *)data)[j] = 0xbe block: 0xbe Debug 1
Debug 0 i: 0x28 j: 0x7 data: 0x1d0fdde0 ((unsigned int )data +j) =
0x1d0fdde7 ((unsigned int *)data)[j] = 0xef block: 0xef Debug 1
Debug 0 i: 0x32 j: 0x8 data: 0x1d0fdde0 ((unsigned int )data +j) =
0x1d0fdde8 ((unsigned int *)data)[j] = 0xca block: 0xca Debug 1
Debug 0 i: 0x36 j: 0x9 data: 0x1d0fdde0 ((unsigned int )data +j) =
0x1d0fdde9 ((unsigned int *)data)[j] = 0xfe block: 0xfe Debug 1
Debug 0 i: 0x40 j: 0x10 data: 0x1d0fdde0 ((unsigned int )data +j) =
0x1d0fddea ((unsigned int *)data)[j] = 0x0 block: 0x0 Debug 1
Debug 0 i: 0x44 j: 0x11 data: 0x1d0fdde0 ((unsigned int )data +j) =
0x1d0fddeb ((unsigned int *)data)[j] = 0x0 block: 0x0 Debug 1
Debug 0 i: 0x48 j: 0x12 data: 0x1d0fdde0 ((unsigned int )data +j) =
0x1d0fddec ((unsigned int *)data)[j] = 0x0 block: 0x0 Debug 1
If you update with more details and/or new result anyone would get more understanding an make an answer better and you would get more solid answer.