Thread: How to pass int array[] to float array[] in a function?

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    8

    How to pass int array[] to float array[] in a function?

    Hi ld like to know how to pass an array of integers to a function that takes an array of integers. The idea l want to print() the data from an array of type integer as well as of type float.

    l made a test with single numbers and it worked without type of casting. But using arrays it gives me zeros.

    If l do casting it do nothing to me

    header:
    Code:
    void test (float _n[], int _length) { //\n
        int i;
        for (i=0;i<_length;i++)
            printf("%.2f\n", _n[i]);
    };
    main:
    Code:
        float a[2] = {5, 9};
        int    b[2] = {2, 4};
    
        test(b, 2); // output 5.00, 9.00
        
        test(a, 2); // output 0.00, 0.00
        test((float*)a, 2); // output 0.00, 0.00
    and also is saying "Suspicious pointer conversion". Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What you want to do is convert each int in the array of ints to a float, i.e., the value of each int would be converted to a corresponding value of float type. Casting the array of int to pointer to float, on the other hand, causes each int in the array of ints to be reinterpreted as a float, i.e., the bits of the int would be treated as if it were the bits of a float, which isn't what you want.

    What I suggest is to create an array of float that is at least as long as the number of elements of the array of int in use, then copy over each of these int values to the array of float, casting each int to float as you go. Then, you pass this array of float as an argument to the function.

    However, if you have control over the design of this function, an even better solution is to simply do this:
    Code:
    void print_as_float(const int values[], size_t length) {
        for (size_t i = 0; i < length; i++) {
            printf("%.2f\n", (float)values[i]);
        }
    }
    Note that the float will be promoted to double though. I used a const int* parameter for the array as printing doesn't involve modification, and I used size_t instead of int because that is typically the appropriate type for a size.

    Having said all that, why would you want to print ints as floats?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The short answer is you can't - at least not in any easy simple way like you could do with function overloading in C++.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    it gives me zeros
    Code:
    printf("%.0f",floatNumber);
    This will not change the number but will cut of the all numbers after decimal point when outputting.
    Last edited by Structure; 02-02-2020 at 03:31 PM.

  5. #5
    Registered User
    Join Date
    Jan 2020
    Posts
    8
    Thanks for the answers, l was suspecting that. The ldea is because l wanted to have the same function to print an array values of type int and for type float too. But l guess that a limitation of the language, unless l do that @laserlight was reffering. Anyway thanks you all people, lm still working hard.

    Here is the correctly main(), by the way.
    Code:
    float a[2] = {5,9};
    int   b[2] = {2, 4};
    
    test(a, 2); // 5.00 | 9.00
    test((float*)b, 2); // 0.00 | 0.00
    test(b, 2); // 0.00 | 0.00

  6. #6
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    plex.c:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    
    struct numberType { 
        int whole ; 
        float decimal ;
    }; struct numberType variable ;
    
    void sift ( int integer, float number ) { 
        char buffer[100] ;
        gcvt (number, 8, buffer) ;
        int last = (strlen(buffer)-1) ;
        if (buffer[last] == '.') buffer[last] = 0 ;
        variable.whole = atoi(buffer) ;
        variable.decimal = atof(buffer) ;
    }
    
    int main ( int argc, char *argv[] ) {
        for (float i=1; i< 4; i+=0.3) {
            sift ( (int)i, (float)i ) ;
            printf( "   { %i }", variable.whole) ;
            printf( "   { %.2f }", variable.decimal) ;
            printf( "   { %.2f }", round(variable.decimal) ) ;
            printf("\n") ;
        }
        return 0 ;
    };
    gcc plex.c -o plex.exe
    plex
    "without goto we would be wtf'd"

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Structure View Post
    plex.c:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    
    struct numberType { 
        int whole ; 
        float decimal ;
    }; struct numberType variable ;
    
    void sift ( int integer, float number ) { 
        char buffer[100] ;
        gcvt (number, 8, buffer) ;
        int last = (strlen(buffer)-1) ;
        if (buffer[last] == '.') buffer[last] = 0 ;
        variable.whole = atoi(buffer) ;
        variable.decimal = atof(buffer) ;
    }
    
    int main ( int argc, char *argv[] ) {
        for (float i=1; i< 4; i+=0.3) {
            sift ( (int)i, (float)i ) ;
            printf( "   { %i }", variable.whole) ;
            printf( "   { %.2f }", variable.decimal) ;
            printf( "   { %.2f }", round(variable.decimal) ) ;
            printf("\n") ;
        }
        return 0 ;
    };
    What's that do?

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Quote Originally Posted by Hodor View Post
    What's that do?
    It's as if an insane person tried to write:
    Code:
    #include <stdio.h>
    #include <math.h>
     
    int main() {
        for (float i = 1; i < 4; i += 0.3)
            printf("%d   %.2f   %.2f\n", (int)i, i, round(i));
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The conversion to string and back feels like a Rube Goldberg machine to me. It would be simpler to write:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    struct numberType {
        int whole;
        float decimal;
    };
    
    int main(void) {
        struct numberType variable;
        for (float i = 1.0f; i < 4.0f; i += 0.3f) {
            variable.whole = (int)i;
            variable.decimal = i;
            printf("   { %i }   { %.2f }   { %.2f }\n",
                   variable.whole, variable.decimal, round(variable.decimal));
        }
        return 0;
    }
    and we arguably don't need struct numberType:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void) {
        for (float i = 1.0f; i < 4.0f; i += 0.3f) {
            int whole = (int)i;
            printf("   { %i }   { %.2f }   { %.2f }\n", whole, i, round(i));
        }
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    The idea l want to print() the data from an array of type integer as well as of type float.
    you can't - at least not in any easy simple way

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    char buffer[128] ;
    
    #define def(i) gcvt (i, 8, buffer) ;
    
    int main( int argc, char *argv[] ) {
    
        int a = 10 ; 
        float b = 11.55 ; 
        double c = 71.432 ;
        
        def(a) ; printf("%s \n", buffer) ;
        def(b) ; printf("%s \n", buffer) ;
        def(c) ; printf("%s \n", buffer) ;
    
    }
    The C Preprocessor: Macros
    Data buffer - Wikipedia
    Last edited by Structure; 02-04-2020 at 06:57 AM.
    "without goto we would be wtf'd"

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Structure View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    char buffer[128] ;
    
    #define def(a) gcvt (a, 8, buffer) ;
    
    int main( int argc, char *argv[] ) {
    
        int a = 10 ; 
        float b = 11.55 ; 
        double c = 71.432 ;
        
        def(a) ; printf("%s \n", buffer) ;
        def(b) ; printf("%s \n", buffer) ;
        def(c) ; printf("%s \n", buffer) ;
    
    }
    The C Preprocessor: Macros
    https://en.wikipedia.org/wiki/Data_buffer
    I don't understand your preoccupation with gcvt: it is a non-standard function that apparently was once part of POSIX but was later removed in favour of sprintf and snprintf. But more importantly, it is overkill: there is absolutely no need to convert to string here.

    Your suggestion of a macro has merit, but since the aim is to print as a float, it can work without the use of a global buffer... which is bad practice especially when it is named just "buffer".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-20-2019, 02:00 PM
  2. Replies: 2
    Last Post: 01-10-2016, 01:23 AM
  3. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  4. Can't pass array to function
    By yougene in forum C Programming
    Replies: 9
    Last Post: 08-19-2007, 04:09 PM
  5. Replies: 3
    Last Post: 04-02-2002, 01:39 PM

Tags for this Thread