The below post is just something i thought of contributing here for young( or old ) members starting out in C.
In (effectively) 3 lines of code , i hope to give an intro about pointer arithmetic and how endian-ness in windows affects the output.
#include<stdio.h>
int main(void){
int a = 66561;
char *c = &a;
int i;
for(i=0 ; i < 4 ; i++) printf("%d\n",*(c+i));
}
This gives an output of
1
4
1
0
Why ?
Well , 66561 in binary is 00000000-00000001-00000100-00000001
. (iv separated each byte with a '-' )
But , in windows , which is a little endian system , the above binary is stored in reverse order ( little end coming first ) in memory as :
00000001-00000100-00000001-00000000.
So , when you go reading one byte at a time , it is read as 1 , 4 , 1 and 0.
However , you'll notice that i did this using a character pointer , not by creating any character array. Well , that's because you dont need to do that.
If u write : int arr[5]
, then arr
is same as saying &arr[0]
they both point to the starting address of the memory occupied by what we take as an array. In the same way , if u say int *a = 5
, then a
is the same as arr
or &arr[0]
. its just a pointer that points to a certain memory.
Applying this concept , when i write : char *c = &a;
what im doing is making the char pointer c
point to the starting address of the 4 byte int a
. Then , when i say printf("%d\n",*(c+i))
, what happens is that in each iteration , the program moves forth by as many bytes as the type of variable c
happens to be. In this case , its 1 byte. So in four steps , the program moves one byte at a time , reading in the same way as it would if it was traversing an array.
In fact , you can just as well write printf("%d",c[1])
instead of printf("%d\n",*(c+i))
, Both mean the same thing to the program : "Go one byte (size of char) at a time from the base address (which is the start of the 4 byte int) "
Lastly , You do get a warning : initialization from incompatible pointer type
when compiling , but thats coz your tricking the program to thing the value contained by the int is actually a char.In more complicated code , these kinda stuff can be hard to debug if a crash occurs , so the compiler warns you before hand.
ps : Experienced C folks out there , do let me know if i made some mistakes. Also feel free to improve the answer :)
Hope this helps someone
Somjit.