jeudi 28 juillet 2016

Why passing 2D matrix gives error in C?

I have written this below simple code to demonstrate passing 2D matrix in C. I have gone through most of the online articles. And this code is inaccordance with those suggested approaches. But still I am getting a compile error. Please explain.

#include <stdio.h>

int max = 0;

void maxId(int i,int j, int p[8][12]){


}

int main(){
    int m = 8;
    int n = 12;
    int p[m][n] = {{0,1,1,0,1,0,0,0,1,1,1,0},
                 {0,1,1,0,1,0,0,0,1,1,1,0},
                 {0,0,1,0,1,0,0,0,1,1,1,0},
                 {0,1,0,0,1,1,1,0,1,1,1,0},
                 {0,0,1,0,1,0,1,0,1,1,1,0},
                 {0,0,1,0,1,0,1,0,1,1,1,0},
                 {0,1,0,0,1,1,0,0,1,1,1,0},
                 {0,0,1,0,1,0,0,0,1,0,1,1}};

    int v[m][n];

    for(int i= 0;i<m;i++)
        for(int j = 0;j<n;j++){
            v[i][j] = v[i][j];
        }

    maxId(0,0,p);
    return 0;
}

Updated

The compile error shown is

29 13 C:UsersRainDropDocumentsdevc++ projectsUntitled2.cpp [Error] cannot convert 'int ()[n]' to 'int ()[12]' for argument '3' to 'void maxId(int, int, int (*)[12])'

Aucun commentaire:

Enregistrer un commentaire