-
Float to Double error
I am having issues with the following function, which receives an array of double values and and integer, n, which is the number of values in the array.
Code:
void something (double[] numbers, int n){
int i;
for (i=0; i<n; i++)
printf ("%f ", numbers[i]);
}
I tried passing the following array of float values to the function:
Code:
float nums[3] = {1.00, 2.00, 3.00};
something(nums, 3);
However, when the values in the array are printed, the output is:
2.00000 0.00000 0.00000
I want to be able to pass arrays of float values as well as arrays of double values to the function.
Any ideas what could be causing this problem?
Thanks!
-
An array of float is not the same thing as an array of double. You cannot pass one when a function is expecting the other. Your compiler should have complained when you tried this, unless you didn't prototype the function before using it (which you always, always should do).
Pass an array of double, or have your function expect an array of float (technically it's pointer to float vs pointer to double, but the fix is the same).
-
cas is right.
"I want to be able to pass arrays of float values as well as arrays of double values to the function."
-- If you really want to do so, you must use function overriding in C++.
BTW:
There is a mistake in double[] numbers. [] should not have been placed before numbers.
Sorry for my poor English.