c++ dynamic array initialization with declaration

后端 未结 4 826
广开言路
广开言路 2020-12-03 09:12

I have function like this:

void findScarf1(bool ** matrix, int m, int n, int radius, int connectivity); 

and in main funct

4条回答
  •  情歌与酒
    2020-12-03 09:26

    My Following example may be helpful for you:

    #include
    void f(int (*m)[7]){  // As Nawaz answred
     printf("%d\n", m[3][3]);
    
    }
    void _f(int m[6][7]){ // As I commented to your question
     printf("%d\n", m[3][3]);
    
    }
    void _f_(int m[][7]){// Second form of Nawaz's answe 
     printf("%d\n", m[3][3]);
    
    }
    void f_(int (*m)[6][7]){// Pointer of 2D array
     printf("%d\n", (*m)[3][3]);
    }
    
    int main(){
    
        int matrix[6][7] = {
        {0, 0, 1, 1, 1, 0, 0},
        {0, 0, 1, 3, 1, 0, 0},
        {0, 0, 1, 4, 1, 0, 0},
        {0, 0, 1, 5, 1, 0, 0},
        {0, 0, 1, 6, 1, 0, 0},
        {0, 0, 1, 7, 1, 0, 0}
        };
        f(matrix);
        _f(matrix);
        _f_(matrix);    
        f_(&matrix);
        return 1;
    }    
    

    question not tanged to c, but I compiled with gcc (I have not installed g++).

    ~$ gcc  -Wall -pedantic 2d.c
    ~$ ./a.out 
    5
    5
    5
    5
    

    I was not intended to post an answer, but because I commented wrong to Nawaz answer so during an experiment I written this code.

    Here one can find it working at codepacde

提交回复
热议问题