#include <stdio.h>
int main(void)
{
int x = 1000;
char *ptr = &x;
printf("%d\n",*ptr);
return 0;
}
Output: -24
/*In gcc 4.4.3 in Ubuntu 10.04 OS*/
With a warning: initialization from incompatible pointer type
What I think if the base type of the pointer were of int type then it would have retrieved 4 bytes from the location it's pointing.Then the O/P would have been 1000.But as I changed the base type to char then it would retrieve 1 byte from location it's pointing when I dereference it.But how the answer is -24. Again when I changed the program as below
#include <stdio.h>
int main(void)
{
int x = 1000;
float *ptr = &x;
printf("%f\n",*ptr);
return 0;
}
The output becomes 0.000000 with same warning.when I derefence the pointer it would retrieve 4 bytes from the location it's pointing.But how the O/P is 0.000000.I'm bit confused.Can u guys plz explain it.I new in C programming, so any mistake in asking the question, plz forgive me. Thanx