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

  1. you dereferencing int , casting float in

    printf("(float)* : %f\n", (float)*iptr); 

    which fine.

  2. you casting int pointer float pointer, , printing float pointer "%f" specifier undefined behavior, correct specifier printing pointers "%p", so

    printf("(float*) : %f\n", (float*)iptr); 

    is wrong, should be

    printf("(float*) : %p\n", (void *) iptr); 

    casting float * here not meaningful, because void * address same float * address , int * address, difference when pointer arithmetic.

  3. you casting int pointer float , dereferencing resulting float pointer, although violate strict aliasing rules in

    printf("(float*) : %f\n", *(float*)iptr); 

    which undefined behavior


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -