pointers - Difference between (float *) & *(float*) in C -
i trying understand pointer concepts in-depth. in following code,
#include <stdio.h> int main() { int = 10; int *iptr = &i; printf("(float)* : %f\n", (float)*iptr); printf("(float*) : %f\n", (float*)iptr); printf("*(float*) : %f\n", *(float*)iptr); return 0; } output:
(float)* : 10.000000 (float*) : 10.000000 *(float*) : 0.000000 i got warning type-cast (float*).
find difficult analyse difference. if can me analyse exact usage of three, helpful.
the difference is
you dereferencing
int, castingfloatinprintf("(float)* : %f\n", (float)*iptr);which fine.
you casting
intpointerfloatpointer, , printingfloatpointer"%f"specifier undefined behavior, correct specifier printing pointers"%p", soprintf("(float*) : %f\n", (float*)iptr);is wrong, should be
printf("(float*) : %p\n", (void *) iptr);casting
float *here not meaningful, becausevoid *address samefloat *address ,int *address, difference when pointer arithmetic.you casting
intpointerfloat, dereferencing resultingfloatpointer, although violate strict aliasing rules inprintf("(float*) : %f\n", *(float*)iptr);which undefined behavior
Comments
Post a Comment