how to pass array of int to a function that accept array of float

Viewed 56

I want pass an array of int to an array that have array of float parameter this is what I tried, it do compile but it give rubbish results !

function_1(float arr*, int len)
{
  ....
  ....
}

main()
{
     int samp[5] = {-1, 3, 5, 10, 20};
     function_1((float *)samp, 5); //not correct !
}
1 Answers

You can't. No computer have the same internal and bitwise representation of float and int. Not to mention that sizeof(float) doesn't have to be equal to sizeof(int).

The only solution is to create a new array, and convert each and every element from int to float, and then pass the proper array of float.

Related