Just i was testing my understanding of pointers, while doing so i did this,
Okay this is what i think.
All pointers to any type hold addresses, right?
Say i declare
char *a, b = 'f';
a = &b ;
So when i try to access content of b that is &b, through a pointer to type char.which is okay.
But, What if i store address of int data type in a pointer of char type(although i get warning: assignment from incompatible pointer type [-Wincompatible-pointer-types], i'm not doing wrong, right?.i'm still storing address of some memory.)
int main(){
// int type of one byte
int a, i ;
char *b;
for(i = -256 ;i < 257; ++i){
a = i;
b = &a;
printf("for i = %d, value stored in first byte of a = %d\n",i ,*b);
}
return 0;
}
now i thought that it will be allowed to read only first byte of 4 bytes allocated to a because i'm accessing through a pointer to a char type, i was expecting to store values up to 255, beyond this will be kind of overflow.
but it happened something different i could only store 127 to -128 whao!!.Did you see it a type int of size one byte.You can run code to see.
Now assume that int type is of size one byte
when a = 256, value read is 0, which is expected, because 0001 0000 0000 it will read only first byte.
Output is predictable until we reach a = 127, which is stored as 0111 1111 (which is positive value from type int perspective)
when a = 128, output is -128 , which is stored 1000 0000 (which is indeed -128)
similarly other outputs can be explained
So output can be explained by assuming that int type is of size one byte, why did printf do that instead of throwing error.
So what exactly did printf do here?
Thank you excellencies ;)