0

I already know some matching a parameter with its argument in multidimensional array.

If I declare a multidimensional array like this:

void print(int (*a)[2], ...)    //-----(1)
{ }

void print(int *a, ...)         //-----(2)

int main(){
    int arr[3][2]={ {1,2},{3,4},{5,6} };

    print(a, ...)                  //-----(1)
    print(a[2], ...)               //-----(2)
}

I'm studying about dynamically creating an multidimensional array. Its code:

void print(int **a, ...)
{}

int main()
{
    int row = 3;
    int col = 2;
    int sample[3][2]={ {1,2},{3,4},{5,6} };

    int **twostar = new int*[row];
    for(int i = 0; i < row; i++)
    {
        arr[i] = new int[col];
        memset(twostar[i],0,col*sizeof(int));
    }

    print(twostar, ...)
}

This above code is working well. and I applied this parameter type to the code without dynamically allocating of a multidimensional array.:

void print(int **a, int m, int n);
int main()
{
    int mat[][2] = { {1,2},{3,4},{5,6} };
    print(mat, 3, 2);       //<<---- error in mat!
}
void print(int **a, int m, int n)
{
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
}

I thought that a pointer type matches an array. Doesn't 2-dimensional array match ** type?

Dean Seo
  • 5,486
  • 3
  • 30
  • 49
  • 3
    [This old answer of mine](https://stackoverflow.com/questions/18440205/casting-void-to-2d-array-of-int-c/18440456#18440456) should hopefully explain why they don't match. – Some programmer dude Feb 14 '18 at 04:33
  • In Mat[3][2], think of it as [int, int| int, int|int, int], and Mat is a pointer to its first element. – John Z. Li Feb 14 '18 at 04:38
  • When I code like this: cout << **mat; << this code print out '1' in console . So I thought mat is acceptable to function parameter ** type. My thought has a problem?? – CHANGJUN KIM Feb 14 '18 at 05:39

0 Answers0