I have this program in C:
int main(int argc, char *argv[]) { int i=300; char *ptr = &i; *++ptr=2; printf("%d",i); return 0; }
Conclusion - 556 at the small end.
I tried to figure out a way out. Here is my explanation.
Question: will there be an answer to the rest on the big end machine?)
i = 300; => i = 100101100 // in binary format in word format => BB Hb 0001 00101100 where B = byte and Hb = half byte
(A) => in memory (assuming it's a small end))
0x12345678 - 1100 - 0010 ( Is this correct for little endian) 0x12345679 - 0001 - 0000 0x1234567a - 0000 - 0000 0x1234567b - 0000 - 0000
0x1234567c - The location of the next intezer (the location of ptr ++ or ptr + 1, where ptr is an intezer pointer, since ptr is of type int => when ++ ptr is executed, it will increase by 4 bytes ( int size ))
when
(B) we do char * ptr = & i; ptr will become of type char => when ++ ptr is executed it will increase by 1 byte ( char size ) so when ++ ptr is executed it will move to the location β 0x12345679 (which has 0001 - 0000) now we do ++ ptr = 2 = > 0x12345679 will be overwritten 2 => 0x12345679 will have 00 * 10 ** - 0000 instead of 000 * 1 * - 0000
therefore, the new memory contents will look like this:
(FROM)
0x12345678 - 1100 - 0010 0x12345679 - 0010 - 0000 0x1234567a - 0000 - 0000 0x1234567b - 0000 - 0000
which is equivalent => BB Hb 0010 00101100, where B = bytes and Hb = half bytes
Am I reasoning correctly? Is there any other short method for doing this? Rgds, Softy