Typecasting int pointer to float pointer

12,561

Solution 1

This is not correct. int and float are not guaranteed to have the same alignment.

Remember: Casting a value and casting a pointer are different scenarios. Casting a pointer changes the way to refer to the type value, which can almost certainly result in a mis-alignment in most of the cases.

As per C11 standard document, chapter §6.3.2.3

A pointer to an object type may be converted to a pointer to a different object type. If the resulting pointer is not correctly aligned68) for the referenced type, the behavior is undefined.

In your case, a work-around may be

printf("%f\n", (float)*a);  //cast the value, not the pointer

Solution 2

You cannot cast a pointer to int to a pointer to float, and expect to get your value converted to the corresponding number in floating point representation. Casting a single value works, but casting by changing a pointer type does not alter the representation.

If you need an array of floats, declare an array of floats, and cast one element at a time:

float b[8];
for (int i = 0 ; i != 8 ; i++) {
    b[i] = a[i];
}
func_expects_float(b, 8);
Share:
12,561

Related videos on Youtube

lukieleetronic
Author by

lukieleetronic

Updated on June 04, 2022

Comments

  • lukieleetronic
    lukieleetronic over 1 year

    I'm trying to do the following

    int a[8]={1,2,3,4,5,6,7,8};
    
    printf("%f\n", *(float *)a);
    printf("%f\n", *((float *)a+1));
    printf("%f\n", *((float *)a+2));
    printf("%f\n", *((float *)a+3));
    printf("%f\n", *((float *)a+4));
    printf("%f\n", *((float *)a+5));
    printf("%f\n", *((float *)a+6));
    printf("%f\n", *((float *)a+7));
    

    I get

    0.000000
    0.000000
    0.000000
    0.000000
    0.000000
    0.000000
    0.000000
    0.000000
    

    The reason why I'm trying to print the elements in this way is because, I want to cast the int pointer to the array to the float pointer and pass it as a parameter for another function which only takes float *.

    It seems that this does not work well. Can someone explain why this is not working?

    int *ptr;
    function((float *)ptr);
    

    If I do this the function does not read the values the pointer is pointing to properly.. just returning 0.0000.

    • BLUEPIXY
      BLUEPIXY over 8 years
      What is your the expected value?
    • Ed Heal
      Ed Heal over 8 years
      Why are you doing this?
  • infinite loop
    infinite loop over 6 years
    The question is asked for C, not C++
  • Sourav Ghosh
    Sourav Ghosh over 6 years
    @infiniteloop If I am not in an urgent need to book a visit to an ophthalmologist, I don't see any mention of CPP in my answer and I believe I don't need to book a visit either. clarification, please?
  • infinite loop
    infinite loop over 6 years
    Sincere apologies @Sourav. I overlooked it.
  • Sourav Ghosh
    Sourav Ghosh over 6 years
    @infiniteloop never mind, and I believe we all have a little space for humor, hope I did not offend you either, right?
  • infinite loop
    infinite loop over 6 years
    No not at all :), that was my mistake. Between on gcc 4.8.5 - 64bit, where size of int and float are 4 bytes, when I tried to convert int value to float through float * as above, store it in float and again back to another int variable. I could get the actual data. According to my observation, printf() in C could not actually print very low values of int (I mean to say 4 byte value stored as an integer not float), so it is printing 0. Increasing the value of int and gives a correct result.
  • infinite loop
    infinite loop over 6 years
    According to my observation, printf() in C could not actually print very low values of int *as float*(I mean to say 4 byte value stored as an integer not float), so it is printing 0.000000. Increasing the value of int,gives a correct result (I mean float value corresponding to int value, i.e., int of 10 doesn't gives 10.000000, rather it gives value depending on method used to implement float in the compiler).