I am using dev cpp on windows7 to compile my code.
int d = 0x12; char* e = (char*)&d; printf("%d %d\n", sizeof (int), sizeof (float)); printf("%p %p\n", &d, (float*)&d); printf("%p %p %p %p %p\n", &d, &e[0], &e[1], &e[2], &e[3]); printf(" %d | %x | %#1x | %#1x | %#1x |%p\n", d, e[0], e[1], e[2], e[3], &e[0]); getchar(); 4 4 0028FF40 0028FF40 0028FF40 0028FF40 0028FF41 0028FF42 0028FF43 18 | 12 | 0 | 0 | 0 |0028FF40 You an see that if I use %d for printing d, it is printing the 4 bytes of e fine. But if I use %f like below, it shows zeros in the place where the first byte of e have to be printed. Anyone can help with why this happens? Why should e's contents depend on how d is formatted?
int d = 0x12; char* e = (char*)&d; printf("%d %d\n", sizeof (int), sizeof (float)); printf("%p %p\n", &d, (float*)&d); printf("%p %p %p %p %p\n", &d, &e[0], &e[1], &e[2], &e[3]); printf(" %f | %x | %#1x | %#1x | %#1x |%p\n", d, e[0], e[1], e[2], e[3], &e[0]); getchar(); The output is:
4 4 0028FF40 0028FF40 0028FF40 0028FF40 0028FF41 0028FF42 0028FF43 0.000000 | 0 | 0 | 0 | 0x28ff40 |76869F1D
(int*)to(float*)? Programming is hard enough as it is without stuff like this.