Return array in a function

后端 未结 19 2288
清酒与你
清酒与你 2020-11-22 05:23

I have an array int arr[5] that is passed to a function fillarr(int arr[]):

int fillarr(int arr[])
{
    for(...);
    return arr;
         


        
相关标签:
19条回答
  • 2020-11-22 06:03

    C++ functions can't return C-style arrays by value. The closest thing is to return a pointer. Furthermore, an array type in the argument list is simply converted to a pointer.

    int *fillarr( int arr[] ) { // arr "decays" to type int *
        return arr;
    }
    

    You can improve it by using an array references for the argument and return, which prevents the decay:

    int ( &fillarr( int (&arr)[5] ) )[5] { // no decay; argument must be size 5
        return arr;
    }
    

    With Boost or C++11, pass-by-reference is only optional and the syntax is less mind-bending:

    array< int, 5 > &fillarr( array< int, 5 > &arr ) {
        return arr; // "array" being boost::array or std::array
    }
    

    The array template simply generates a struct containing a C-style array, so you can apply object-oriented semantics yet retain the array's original simplicity.

    0 讨论(0)
  • 2020-11-22 06:03

    to return an array from a function , let us define that array in a structure; So it looks something like this

    struct Marks{
       int list[5];
    }
    

    Now let us create variables of the type structure.

    typedef struct Marks marks;
    marks marks_list;
    

    We can pass array to a function in the following way and assign value to it:

    void setMarks(int marks_array[]){
       for(int i=0;i<sizeof(marks_array)/sizeof(int);i++)
           marks_list.list[i]=marks_array[i];
    }
    

    We can also return the array. To return the array , the return type of the function should be of structure type ie marks. This is because in reality we are passing the structure that contains the array. So the final code may look like this.

    marks getMarks(){
     return marks_list;
    }
    
    0 讨论(0)
  • 2020-11-22 06:04

    In this case, your array variable arr can actually also be treated as a pointer to the beginning of your array's block in memory, by an implicit conversion. This syntax that you're using:

    int fillarr(int arr[])
    

    Is kind of just syntactic sugar. You could really replace it with this and it would still work:

    int fillarr(int* arr)
    

    So in the same sense, what you want to return from your function is actually a pointer to the first element in the array:

    int* fillarr(int arr[])
    

    And you'll still be able to use it just like you would a normal array:

    int main()
    {
      int y[10];
      int *a = fillarr(y);
      cout << a[0] << endl;
    }
    
    0 讨论(0)
  • 2020-11-22 06:04

    This is a fairly old question, but I'm going to put in my 2 cents as there are a lot of answers, but none showing all possible methods in a clear and concise manner (not sure about the concise bit, as this got a bit out of hand. TL;DR

    0 讨论(0)
  • 2020-11-22 06:06

    and what about:

    int (*func())
    {
        int *f = new int[10] {1,2,3};
    
        return f;
    }
    
    int fa[10] = { 0 };
    auto func2() -> int (*) [10]
    {
        return &fa;
    }
    
    0 讨论(0)
  • 2020-11-22 06:08
    int *fillarr(int arr[])
    

    You can still use the result like

    int *returned_array = fillarr(some_other_array);
    if(returned_array[0] == 3)
        do_important_cool_stuff();
    
    0 讨论(0)
提交回复
热议问题