invalid conversion from 'int' to 'int*' [-fpermissive] on passing array

前端 未结 3 445
野趣味
野趣味 2021-01-25 03:26

I am new to C++ language and I have no idea of pointers and their usage. I\'m facing the error \"[Error] invalid conversion from \'int\' to \'int*\' [-fpermissive]\"

3条回答
  •  自闭症患者
    2021-01-25 04:02

    Change the function call

    cout << midd(ax, asize) << endl;
    

    and declaration

    double midd(int arr[], int size) { ... }
    

    or go the C++ way: std::vector or std::array. For example

    #include 
    #include 
    using namespace std;
    
    double midd(array a) {
        int mid = a.size() / 2;
        return (a[mid] + a[mid + 1]) / 2.0;
    }
    
    int main() {
        array ax = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        cout << midd(ax) << endl;
    }
    

提交回复
热议问题