Return an array in c++

前端 未结 11 2177
半阙折子戏
半阙折子戏 2021-01-02 12:14

Suppose I have an array

int arr[] = {...};
arr = function(arr);

I have the function as

 int& function(int arr[])
 {
//         


        
11条回答
  •  旧时难觅i
    2021-01-02 12:21

    • An array as argument is actually a pointer. Arrays are automatically 'casted' to pointers if given as argument
    • You can't assign an array to another array in C/C++. You will have to use a memcpy or loop to copy the values.
    • If function changes the arr argument, it actually changes the value of the arr variable given by the caller. Again because the array is actually a pointer. So in the caller arr is assigned to arr, which is useless here.

提交回复
热议问题