Return an array in c++

前端 未结 11 2221
半阙折子戏
半阙折子戏 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条回答
  •  梦谈多话
    2021-01-02 12:22

    Following works and doesn't seem messy or mis-understandable!

    int* returnarray(int a[])
    {
        for (unsigned int i = 0; i < 3; i++)
        {
            a[i] *= 2;
        }
        return a;
    }
    
    int main(int argc, char* argv[])
    {
        int arr[3] = {1,2,3};
        int* p = returnarray(arr);
        for (unsigned int i = 0; i < 3; i++)
        {
            cout << "p[" << i << "] = " << p[i] << endl; 
        }
            cin.get();
            return 0;
    }
    

提交回复
热议问题