A little code that clarifies the whole * and & in C that confuses a few !
Snip-A::
#include <stdio.h>
main()
{
int i = 30;
*(char *)&i = 234;
printf("%d",i);
}
In the above code i is defined as an integer variable telling the compiler to set aside 2 bytes of memory and place 30 in their, lets not getting the big-endian or little-endian way of bit-patterns in here, I'll simply assume simple binary representations in-memory
it would look something like this in the memory:
| 00000000 00011110 | ----> 0x1004 // those are 2 bytes in memory at address 1004 to represent i holds 30
Lets evaluate the more interesting statement which comes next
&i // this is simple get the address of i which is 1004
while the compiler is looking to reference this address as what ? it gets that this is a character reference
For simplification, I'll use only code snips further to explain what happens in the computer memory (RAM) when we play around with *'s and &'s (a.k.a referencing and dereferencing)
No comments:
Post a Comment