In: Computer Science
modify the program to cast vptr as a float and as a double
build and run your program
THIS IS THE PROGRAM CODE:
#include <stdio.h>
void main (void)
{
int intval = 255958283;
void *vptr = &intval;
printf ("The value at vptr as an int is %d\n", *((int *) vptr));
printf ("The value at vptr as a char is %d\n", *((char *) vptr));
}
Code:
#include <stdio.h>
void main (void)
{
int intval = 255958283;
void *vptr = &intval;
printf ("The value at vptr as an float is %f\n", *((float *) vptr));
printf ("The value at vptr as a double is %lf\n", *((double *) vptr));
}
Format specifier of float is %f just changed that in program
and format specifier of double is %lf
to type caste void to double just change the datatype to float
*((float *) vptr)
and same for double
*((double *) vptr)
Note:
here the output will not result 255958283.000000
because the type of intval is int and you are using float pointer(type casted) to access the value of intval.
But if the variable intval is float type and you are using float pointer then offcourse your output will result 255958283.000000
Screenshot:
