Thread: Different ways of "Returning" more than one value from a function

  1. #1
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181

    Different ways of "Returning" more than one value from a function

    Suppose a function expects void argument and returns an array. But along with returning this array, we also want to pass something else. Lets say for this example some kind of exit status or flag a 1 or 0.
    What ways are there to do this?

    Code:
    char* foo(void)
    {
        static char word[MAX_LENGTH];
        ...
        return word;
    }
    We could make a structure or array within this function that is composed of word and flag and return that. But that's complex.
    We could make the flag a global variable but global variables are deemed a last resort.
    We could also pass, to this function, instead of void, a pointer to this flag so that when the function ends, the caller pointer has the value. But this requires unwanted modification to the callers code.

    Are there any other ways?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't return arrays. You can return a structure that contains an array. You can return a pointer to a static array declared inside the function. You can return a pointer to some dynamically allocated memory, but you cannot return an array.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Nope... AND C does not know how to return an array either... What you get back is a pointer to an array...

    Here's a little demonstration of why you shouldn't do that...

    Compile this and run it both with and without the "static" keyword, the results may surprise you...
    Code:
    #include <stdio.h>
    
    int* MyFunction(int a, int b, int c)
      {  static int array[3];
         array[0] = a;
         array[1] = b;
         array[2] = c;
         return array;  } // return a pointer.
    
    
    int main (void)
      { int *a1, *a2;  // int pointers
    
        printf("calling a1 = MyFunction(10,20,30);\t");
        a1 = MyFunction(10,20,30);
        printf("a1 has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        printf("calling a2 = MyFunction(100,200,300);\t");
        a2 = MyFunction(100,200,300);
        printf("a2 has %d %d %d\n",a2[0],a2[1],a2[2]);
    
        printf("\nLooks good, except...\t"); 
        printf("a1 now has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        getchar();
        return 0; }
    It doesn't matter if the array is int, char, double, whatever... the result will be the same.

  4. #4
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    I meant return pointer to the array. Whoops. Believe me I learned the hard way. Took me forever to figure out using static. Anyway, so if there is no other way, which of those three methods is 'best' under what scenarios?

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Vespasian View Post
    I meant return pointer to the array. Whoops. Believe me I learned the hard way. Took me forever to figure out using static. Anyway, so if there is no other way, which of those three methods is 'best' under what scenarios?
    Did you even bother to try my example code?

    Even returning static arrays DOES NOT work.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > We could also pass, to this function, instead of void, a pointer to this flag so that when the function ends,
    > the caller pointer has the value. But this requires unwanted modification to the callers code.
    Presumably the caller would be wanting to USE this mysterious new return flag in some way.
    Changing the function prototype is a GOOD way of finding ALL the places where you call foo() from, so you can ensure that ALL of them do the new right thing.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fopen returning "Bad File Pointer"
    By jprukop in forum C Programming
    Replies: 4
    Last Post: 04-17-2009, 03:01 PM
  2. Replies: 3
    Last Post: 03-27-2008, 11:44 PM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM