Return array in a function

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

    Actually when you pass an array inside a function, the pointer to the original array is passed in the function parameter and thus the changes made to the array inside that function is actually made on the original array.

    #include 
    
    using namespace std;
    
    int* func(int ar[])
    {
        for(int i=0;i<100;i++) 
            ar[i]=i;
        int *ptr=ar;
        return ptr;
    }
    
    
    int main() {
        int *p;
        int y[100]={0};    
        p=func(y);
    
        for(int i=0;i<100;i++) 
            cout<

    Run it and you will see the changes

提交回复
热议问题