Thread: returning an array

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    2

    returning an array

    //This code is supposed to accept a decimal integer and print out the number in binary by calling a function that returns the binary number in an array//
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    char *int_to_binary(int x);
    int main()
    {
    int dec;
        printf("Please enter an integer/n");
        scanf("%d", &dec);
        int_to_binary( dec );
            printf("%s\n", int_to_binary(17));
        return 0;
    }
    char *int_to_binary(int x)
    {
    int a;
    char bin[17];
    int_to_binary=bin;
        for( a=0; a<17; a++){
            if (x%2==0)
            {
                bin[a]='0';
            }
            else
            {
                bin[a]='1';
            }
            x=x/2;
        }
    return * int_to_binary;
    }
    my compiler is telling me that I can't use my int_to_binary pointer to point to my bin array. How else can I use the pointer to return an array to main? Please HELP thank you!

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Define the array in main. Pass it to int_to_binary as a parameter, where it will be filled in.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Is there any real benefit to return an array?

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    Code:
    int_to_binary=bin;
    int_to_binary is the name of the function, so in this line you are trying to assign a char pointer to an immutable function pointer. you don't need to do that and it won't compile.take that line out, do what oogabooga said and return the input parameters as the output.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-02-2010, 06:33 AM
  2. Help with returning an array
    By HIT_Braga in forum C Programming
    Replies: 16
    Last Post: 04-09-2010, 03:24 AM
  3. Returning Array
    By Suchy in forum C Programming
    Replies: 4
    Last Post: 03-02-2008, 11:05 PM
  4. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  5. returning array?
    By oomen3 in forum C Programming
    Replies: 4
    Last Post: 10-25-2002, 10:13 PM

Tags for this Thread