Return array in a function

后端 未结 19 2325
清酒与你
清酒与你 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:10

    As above mentioned paths are correct. But i think if we just return a local array variable of a function sometimes it returns garbage values as its elements.

    in-order to avoid that i had to create the array dynamically and proceed. Which is something like this.

    int* func()
    {
      int* Arr = new int[100];
      return Arr;
    }
    
    int main()
    {
      int* ArrResult = func();
      cout << ArrResult[0] << " " << ArrResult[1] << endl;
      return 0;
    } 
    
    
    
    
    
    0 讨论(0)
提交回复
热议问题