Hi everyone,
I learn type casting of pointers.
Could someone explain me why the last printf prints 0.00? I expect to print 10. What I am doing wrong?
Code:
#include <stdio.h>
int main()
{
int a, *Pa;
float b, *Pb;
double c, *Pc;
char d, *Pd;
Pa = &a; // Pa points the address of a
Pb = &b; // Pa points the address of b
Pc = &c; // Pa points the address of b
a = 10;
b = 2.25;
printf("The address of a is %d\n", Pa);
printf("The value of a is %d\n", *Pa);
printf("The address of Pa is %d\n", &Pa);
// What is we want to assign the value of Pa to Pb? Pa is a variable of type int while Pb is a variable of type float
// We have to use type-casting
Pb = (float *)Pa;
printf("The value of b is %.2f\n", *Pb); // Dereferencing Pb to read the value stored in the address the Pb pointer points
return 0;
}