Thread: Correct way to return arrays from functions

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    5

    Correct way to return arrays from functions

    This may sound like a stupid question, but i'm stuck on it.

    How do i properly return an array (the whole array, not just an element of it) from a function?

    the piece of code i have goes like this:

    Code:
    int function(int v[]){
     .
     .
     .
     return v[];
    }
    the program is too long to test it out, but i have made the same test on smaller programs and it didnt work. The sentence "return v;" also doesnt work.

    What i need to do is return the whole array (all of its elements) to the main() function.


    thanks
    hope ppl answer fast

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Since arrays are passed as a pointer to begin with, there is nothing to return as such.

    Any changes you made to the array inside the function will be visible in the array passed by the caller when it called your function.

    Compare with say fgets() or strcpy() which modify the array you pass to them.
    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.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    As Salem said, there's no reason to return the array in your example. But, if you would still like to know how, then make the return function definition: int* and, make the return statement without the squared brackets: return v;

  4. #4
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    You say you want to return an int array, int[], like your parameter but then why have you defined your array to return an int? Return types are declared in the same way as any other types.
    Code:
    int[] function(int v[]){
     return v;
     /*
      * Note that you're only returning v. It's uncessary and erroneous to tell that
      * it's an array, i.e. v[], because the compiler already knows since
      * you told it v's type in the function head.
      */
    }
    Also, as it has been told you're only passing a pointer and not a whole array but unless you say what the function does I can't tell if it really matters or not.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by OnionKnight
    You say you want to return an int array, int[], like your parameter but then why have you defined your array to return an int? Return types are declared in the same way as any other types.
    Code:
    int[] function(int v[]){
     return v;
    }
    I don't suppose you ran that past your compiler?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    No I didn't. I get an error now though. I was pretty sure you could have an array as return type, not sure where I that from.
    But in any case int[] can be changed to int* and that will compile.

    Edit:
    Code:
    typedef int array[5];
    
    array function (array v)
    {
    	return v;
    }
    
    int main ()
    {
    	array v;
    	function(v);
    	return 0;
    }
    Typedefing it gives more descriptive errors.
    Code:
    Error E2091 test.c 4: Functions cannot return arrays or functions
    Error E2110 test.c 5: Incompatible type conversion in function function
    Warning W8057 test.c 6: Parameter 'v' is never used in function function
    It does kind of make sense, since in the return statement v gets transformed into an int pointer so you'd have no choice but to use int*, which I always use anyways. (°3°)
    Last edited by OnionKnight; 04-28-2006 at 11:32 AM.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    sorry, but let me see if i got it right: since arrays are actually pointers all function calls i make will modfy the array as if i had passed it as a pointer? wel, sorry, that sentence may not make much sense. What i'm actually asking is: i dont need to return an array from a function? being that, i could call it a void function and it would still modify the values of the array?

    thx again

    i'll try it later on...

  8. #8
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Yes if your function's purpose with 'v' is to modify it then you do not have to return it.

    Rant about arrays:
    Arrays are not actually pointers, but in most cases they behave the same way. This might be a bit confusing for most people. Arrays are high level constructs, their functionality is not the same at the lower level. Seen from the lower level "return v;" returns a pointer to the start of a memory block but seen from a high level perspective it returns an object of type int[], however since the return type was int* it converts the object to int*. In this case int[] gets implicitly casted to int*. If the compiler could not cast the object to the return type an error is generated. For example trying to return a pointer to something when the return type is float.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Functions that return arrays
    By Scarecrowm in forum C Programming
    Replies: 4
    Last Post: 10-21-2007, 02:08 AM
  4. DirectInput help
    By Muphin in forum Game Programming
    Replies: 2
    Last Post: 09-10-2005, 11:52 AM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM