Can someone please clarify an incorrect interpretation on my part? I know that my understanding is incorrect due my code's resulting output (see the bottom of the question). Thanks in advance.
To clarify, what does each segment of the following line mean?:
*(u8 *)((u32)BufferAddress + (u32)i) And how does it differ from the following line:
*(u32 *)((u32)BufferAddress + (u32)i) My interpretation of the above is:
- segment1 = ((u32)BufferAddress + (u32)i) => determine an address as an integer.
- segment2 = (u32 *)(segment1) => cast the address to be treated like a pointer, where the pointer is 32 bits in length.
- segment3 = *(segment2) => dereference the pointer in order to obtain the value of residing at the calculated address.
What is incorrect about my interpretation? I think my lack of understanding is in the segment2 area... What is the difference between casting (u32 *) and (u8 *)?
Here is the code that made me realize I have a knowledge-gap:
Initialization code:
main(...) { ... u8 *Buffer = malloc(256); ... Buffer[0] = 1; Buffer[1] = 0; Buffer[2] = 0; Buffer[3] = 4; Buffer[4] = 0; Buffer[5] = 0; qFunction(... , Buffer, 6, ...); ... } qFunction(... , const u8 *BufferPointer, u32 BufferLength, ...) { u32 BufferAddress; ... BufferAddress = (u32) BufferPointer; ... /* Method 1: */ for (i=0; i < BufferLength; i++) printf("%d, %p\n", BufferPointer[i], &BufferPointer[i]); /* Method 2: */ for (i=0; i < BufferLength; i++) printf("%d, 0x%lx\n", *(u8 *)(BufferAddress+i), BufferAddress+i); /* Method 3: */ for (i=0; i < BufferLength; i++) printf("%d, 0x%lx\n", *(u32 *)(BufferAddress+i), BufferAddress+i); ... } The outputs of Method 1 and Method 2 are as I expect (both are the same):
1, 0x1000000 0, 0x1000001 0, 0x1000002 4, 0x1000003 0, 0x1000004 0, 0x1000005 However, the output of Method 3 seems weird to me; only part of the result is the same as Method 1/2:
-1442840511, 0x1000000 11141120, 0x1000001 43520, 0x1000002 4, 0x1000003 0, 0x1000004 0, 0x1000005 I'd appreciate any tips or references to reading material. Thanks.