Can we iterate through an array passed to a function using for-each loop?

  arrays, c++, foreach, loops

I know, we can iterate through an array passed as an argument in this way:

    //NO ERROR
    void fun(int *a, int n){
            for(int i=0; i<n; i++)
                cout<<a[i];
        }

  

But, is there any way I could iterate through an array using a for-each loop inside a function like this ??

    //ERROR
    void fun(int *a, int n){
        for(auto x:a)
            cout<<x;
    }

Source: Windows Questions C++

LEAVE A COMMENT