Returning Values in C

This is a discussion on Returning Values in C within the C Programming forums, part of the General Programming Boards category; If I wanted to return an array from one sub program to main in C, how would I do that ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    11

    Returning Values in C

    If I wanted to return an array from one sub program to main in C, how would I do that exactly?

    What I've got for my sub program right now is:


    Code:
    
    float get_inputs(float userInput[4])
    {
            printf("Enter the energy requirements\n");
            scanf("%f", &userInput[0]);
    
            printf("Enter cost of panels, batteries, inverters\n");
            scanf("&f &f &f", &userInput[1], &userInput[2], &userInput[3]);
    
            return userInput[];
    }
    I am wondering if this is the best way to return multiple values from one sup program to another (I am trying to avoid using pointers if I can. If it will make things more complicated, I'll learn how to use pointers.).

    I am also wondering if, in the return statement, I need to put something in between the brackets...

    Finally, how would I properly call the program in main. I'm going to put it in a loop, and I've always had trouble putting in the exact syntax for calling a sub program.

    Thanks in advance.

  2. #2
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    It is not possible to return an array in C.
    You can change the value of an array by passing that array as a parameter in the function call,
    and you can return a pointer to that array, however returning it is not an option.

    Code:
    float *my_function( float array[], int size )
    {
        /* do things here with the array of floats */
        return array; /* return a pointer to array if you want */
    }
    
    
    int main ( void )
    {
        float numbers[4];
    
        /* call to function */
        my_function( numbers, sizeof numbers / sizeof ( float ) );
        .
        . 
        .
    }
    Last edited by Aia; 11-20-2007 at 06:58 PM.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    11
    okay, so I will need to create a pointer to the array, and then return the pointer?

  4. #4
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    In fact, if you are using arrays, you are in fact using pointers. Both are closely related.

    And your code is incorrect. You can't return a collection of value. What you can do is return a pointer and/or write in memory at a certain location where the calling function want you to do so. I'm quite sure the FAQ talks about pointers and arrays, maybe you should take a look.

    So... if you want to do a "multiple return", (let's say we want a function that fill an array of integer with a certain value), here's a couple of equivalent examples

    The "classic" version
    Code:
    int* fillArray(int array[], int nbElem, int value)
    {
       int i;
       for (i = 0; i < nbElem; i++)
       {
            array[i] = value;
       }
    
       return array;
    }
    The "i love pointer" one
    Code:
    int *fillArray(int array[], int nbElem, int value)
    {
       int *ptr;
       ptr = array;
       while (ptr < array + nbElem)
       {
           *ptr = value;
           ptr++;
       }
    
       return array;
    }
    The "recursive" one (hah..)
    Code:
    int *fillArray(int array[], int nbElem, int value)
    {
       if (nbElem < 1)
          return NULL;
    
       *array = value;
       fillArray(array + 1, nbElem - 1, value); 
    
       return array;
    }
    And, last but not least, the x86-Visual Studio-specific one
    Code:
    int *fillArray(int array[], int nbElem, int value)
    {
       __asm
       {
           mov eax, value
           mov edi, array
           mov ecx, nbElem
           rep stosd
       }
    
       return array;
    }
    By the way, don't look at the last 2 versions, especially the last one.

  5. #5
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by cuba06 View Post
    okay, so I will need to create a pointer to the array, and then return the pointer?
    If all that you want to do is to change the values in the array passed as a parameter you don't have to return a pointer to it.
    i.e.
    Code:
    float *my_function( float array[], int size )
    Could be:
    Code:
    void my_function ( float array[], int size )
    that float array[] is a pointer to the first element of the array passed, i.e. &numbers[0], and size is how many elements that array is made of.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Just be sure you don't return a pointer to a local array, otherwise the array is deleted as soon as the function ends.

    Code:
    char* BadFunction()
    {
        char str[20];
        strcpy( str, "Local String" );
    
        return str;  /* BAD!  str is a local array. */
    }
    BTW, it's not a "sub program", it's called a function.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > float get_inputs(float userInput[4])
    In this instance, you don't need to return anything.

    All the data you read in will be in the array you passed as a parameter (arrays are always passed as a pointer to the start of the array).
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-31-2009, 12:53 AM
  2. returning values (tuples)
    By l2u in forum C++ Programming
    Replies: 10
    Last Post: 09-20-2007, 09:24 PM
  3. Struct Values
    By Muphin in forum C++ Programming
    Replies: 5
    Last Post: 08-13-2005, 10:24 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. printf returning values problem
    By hayai32 in forum C Programming
    Replies: 20
    Last Post: 06-25-2005, 01:16 PM

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