Pointer of a two dimensional array of variable dimensions?

Hi, I have some problems with multi dimensional arrays.
I though that in the same way we point to an array by a pointer, I could point to an array of arrays by a pointer of pointers. such that:
1
2
3
char x[AnyNumber][AnyNumber];
char ** p;
p=x;

as a derivative of the fact that:
1
2
3
char x[AnyNumber];
char * p;
p=x;

but it's clear that I'm misunderstanding something and that's not how it works.
Now I think the two dimensional array isn't really a two dimensional array. Instead, it's a single string having one dimension but it's wrapped in a constant width (the second dimension).
If that is true, (and even if it's not) then I have one problem, "How to use two dimensional arrays as a parameter in a function call, where both dimensions might differ from a call to another."

Here is my attempt in an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <windows.h>
#include <iostream>
using namespace std;

void Print(char ** Buffer, COORD BufferCoord)
{
    for(int y=0; y<BufferCoord.Y; y++)
    {
        for(int x=0; x<BufferCoord.X; x++)
        {
            cout<<Buffer[y][x];
        }
        cout<<endl;
    }
}

int main()
{
    char Array1[3][3]={{'1','2','3'},{'a','b','c'},{'A','B','C'}};
    char Array2[5][5]={{'1','2','3','4','5'},{'a','b','c','d','e'},{'A','B','C','D','E'},{'X','Y','Z','H','I'},{'!','@','#','$','%'}};

    cout<<"\nArray1:\n-------------------------\n";
    Print(Array1,{3,3});
    cout<<"\n\nArray2:\n-------------------------\n";
    Print(Array2,{5,5});
}
That is fine, if you have a suitable COORD. A struct that has members X and Y and a constructor that takes {4,2} as parameter.

The alternative is to pass plain integers:
1
2
3
4
5
6
void Print( char ** Buffer, int Rows, int Cols );

int main() {
  // code
  Print( Array1, 3, 3 );
}

Right, but my problem is with the array. it returns the following error for each function call:
Line |23|error: cannot convert 'char (*)[3]' to 'char**' for argument '1' to 'void Print(char**, COORD)'|
Line|25|error: cannot convert 'char (*)[5]' to 'char**' for argument '1' to 'void Print(char**, COORD)'|
if you have a suitable COORD

What constructors does your COORD have?
Thanks guys! now I got it; regarding the dimensions of the array, members are stored in successive addresses.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <windows.h>
#include <iostream>
using namespace std;

void Print(char * Buffer, COORD BufferSize)
{
    for(int y=0; y<BufferSize.Y; y++)
    {
        for(int x=0; x<BufferSize.X; x++)
        {
            cout<<Buffer[(y*BufferSize.X)+x];
        }
        cout<<endl;
    }
}

int main()
{
    char Array1[3][3]={{'1','2','3'},{'a','b','c'},{'A','B','C'}};
    char Array2[5][5]={{'1','2','3','4','5'},{'a','b','c','d','e'},{'A','B','C','D','E'},{'X','Y','Z','H','I'},{'!','@','#','$','%'}};

    cout<<"\nArray1:\n-------------------------\n";
    Print(&Array1[0][0],{3,3});
    cout<<"\n\nArray2:\n-------------------------\n";
    Print(&Array2[0][0],{5,5});
}


And you're right keskiverto. I'm sorry for initializing the COORD in this way. it might not be compatible with all versions of C++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <windows.h>
#include <iostream>
using namespace std;

void Print(char * Buffer, int SizeX, int SizeY)
{
    for(int y=0; y<SizeY; y++)
    {
        for(int x=0; x<SizeX; x++)
        {
            cout<<Buffer[(y*SizeX)+x];
        }
        cout<<endl;
    }
}

int main()
{
    char Array1[3][3]={{'1','2','3'},{'a','b','c'},{'A','B','C'}};
    char Array2[5][5]={{'1','2','3','4','5'},{'a','b','c','d','e'},{'A','B','C','D','E'},{'X','Y','Z','H','I'},{'!','@','#','$','%'}};

    cout<<"\nArray1:\n-------------------------\n";
    Print(&Array1[0][0],3,3);
    cout<<"\n\nArray2:\n-------------------------\n";
    Print(&Array2[0][0],5,5);
}
Topic archived. No new replies allowed.