Return array in a function

后端 未结 19 2654
清酒与你
清酒与你 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 05:49

    $8.3.5/8 states-

    "Functions shall not have a return type of type array or function, although they may have a return type of type pointer or reference to such things. There shall be no arrays of functions, although there can be arrays of pointers to functions."

    int (&fn1(int (&arr)[5]))[5]{     // declare fn1 as returning refernce to array
       return arr;
    }
    
    int *fn2(int arr[]){              // declare fn2 as returning pointer to array
       return arr;
    }
    
    
    int main(){
       int buf[5];
       fn1(buf);
       fn2(buf);
    }
    

提交回复
热议问题