Float to Double error

This is a discussion on Float to Double error within the C Programming forums, part of the General Programming Boards category; I am having issues with the following function, which receives an array of double values and and integer, n, which ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    1

    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!

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    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).

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    China
    Posts
    12
    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.
    Last edited by orientuser; 10-13-2010 at 02:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21