简体   繁体   中英

Error while passing a 2D array to a function

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

void faill (int arry[10][6], int f[], int a, int b)
{
    f={};
    for (int i=0;i<a;i++)
        for(int j=0;j<b;j++)
        if(arry[i][j]<60)
        f[i]++;
}
int main()
{
    srand(time(0));
    int student=10, test=6;
    int grade[student][test];
    int fail[10]={};

    for(int i=0;i<student;i++)
        for(int j=0;j<test;j++)
        grade[i][j]=rand()%101;
       faill(grade[10][6],fail,student,test);

        for (int x=0;x<student;x++)
        {
            cout<<"Student "<<x+1<<" : ";
            for (int y=0;y<test;y++)
                cout<<grade[x][y]<<" ";
            cout<<"(failed "<<fail[x]<<" subjects)"<<endl;
        }
    return 0;
}

This code have the following errors:

|24|error: invalid conversion from 'int' to 'int (*)[6]' [-fpermissive]|

|6|note: initializing argument 1 of 'void faill(int ( )[6], int , int, int)'|

for(int i=0;i<student;i++)
        for(int j=0;j<test;j++)
        grade[i][j]=rand()%101;
       faill(grade,fail,student,test);

when you were passing in grade[10][6] you were passing an int to a piece of memory you didn't allocate. It would have been the 11th and 7th element.

You need to pass the array to that function as shown above.

Give function prototype:

void faill (int arry[10][6], int f[], int a, int b);

then at least your calling code is incorrect:

faill(grade[10][6],fail,student,test);  // grade[10][6] wrong

that because grade[10][6] is an int not an array - which is what you need to parse the function faill

You need to change that to:

faill(grade,fail,student,test);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM