I have a small doubt on typecasting. Here I am pasting my sample code.
#include <stdio.h>
int main()
{
int i=1100;
char *ptr=(char*)&i;
printf("Value of int =%d\n",(int*)(*ptr));
return 0;
}
I have typecast at two points. First one is,
char *ptr=(char*)&i;
Second one is ,
printf("Value of int =%d\n",(int*)(*ptr));
in order to avoid the compilation warnings.
When I print it I got the output as
Value of int =76
I know the fact that we need to assign the address of values to the proper type of pointers in order to deference it properly.
My doubt is,
- Is it possible to print the value 1100 using character pointer ? If Yes then how ?
I expect a clear cut answer from you folks. Please help on this.
*ptris achar, not achar*. It only makes sense to cast a pointer tochar*in order to inspect its contents, and this is the only pointer type that is allowed to alias other pointers (you can use fromvoid*to pass the pointer around, but you cannot inspect its byte representation this way). Casting fromchar*to other pointers is undefined behavior, if you don't make sure that you are casting to the original pointer that was aliased. And it's completely unclear why you would want to use achar*here, since%dexpects anintparameter.