Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typecasting int pointer to float pointer

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.

like image 333
lukieleetronic Avatar asked May 16 '15 14:05

lukieleetronic


People also ask

Can you cast an int to a pointer?

An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.

What happens when you cast int to pointer?

You can convert an integer to pointer, but the resulting pointer value cannot be dereferenced. The right four bytes of such a pointer will contain the original integer value, and this value can be recovered by converting the pointer back to an integer.

Can pointer be float?

Float pointer Data type is the only difference. A float pointer only stores an address of a float variable.

Can you have a float pointer in C?

A Simple Example of Pointers in C Important point to note is: The data type of pointer and the variable must match, an int pointer can hold the address of int variable, similarly a pointer declared with float data type can hold the address of a float variable.


2 Answers

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
like image 125
Sourav Ghosh Avatar answered Sep 22 '22 05:09

Sourav Ghosh


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);
like image 27
Sergey Kalinichenko Avatar answered Sep 20 '22 05:09

Sergey Kalinichenko