As of what I know about ‘&’ operator, it returns the base address of the operand in memory.
Let us imagine the following scenario (as on my machine):
- sizeof(int) = 4 bytes
- sizeof(float) = 4 bytes
- sizeof(char) = 1 byte
Now, if I write something like this:
void main() {
int i = 5411;
int *ip = &i;
char *c = &i;
printf("%d",*ip);
printf("%c",*c);
}
The first printf() should give me 5411. Talking about the second printf(), the base address of i contains 10101001 (higher order 8 bits = 1 byte for char type pointer).
Hence *c should give me 169, which when converted to %c is an invalid character.
But the compiler is giving me ‘#’ or some other valid output. Why is it so ? Any inputs ?
EDIT (taken from the author’s comment on one of the answers):
That was just a dummy case, since I was away from the actual machine.
The actual case is i = 5411
You seems to have trouble understanding how integers are stored in memory. Take 5411 as example.
this number 13 binary digits has however, since an
intis 32-bit, it must be pad to 32 digitsOn a little endian machine (x86, ARM by default), the least significant bytes are stored in the front, so in the memory:
Therefore,
*cshould return 00100011 i.e. 35 ('#').