Does Anyone know how to reference a 2D string array in a function?

So everything works fine for the most part, but I don't know how to use a 2D array as a reference. Any help would be much appericated.

What I currently have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
string Assign_Array(string line, ifstream& in_file)
{

string Grid[size][size];
    string numline;

    if( line == "N")
    {

        for ( int y=0; y<=8; y++)
        {
            getline(in_file,numline);

            for ( int x=0; x<=8;x++)
            {
                Grid[x][y]=numline[x];

            }

        }

    }
return Grid[size][size];
}


I've tried a few different things,and none of them worked, so that is the original.

*Even if someone could show me how to assign the returned value to varible in a different program
Any array can be passed or returned through its pointer, namely, the array name. In your case, it's Grid.
So what I think you are saying is:

1
2
string Assign_Array(string line, ifstream& in_file, string& Grid)


But that has to be wrong, because I can't get that to work :-/
First of I want to say using multidimensional arrays is very cumbersome and I (almost) never use them. It is often easier to use a 1D array/vector and index into that like y*width+x and/or wrap it inside a class.

But if you really want to use them you can pass in the Grid as a parameter like this and make the return type void.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Assign_Array(string line, ifstream& in_file, string Grid[size][size])
{
    string numline;
    if( line == "N")
    {
        for ( int y=0; y<=8; y++)
        {
            getline(in_file,numline);

            for ( int x=0; x<=8;x++)
            {
                Grid[x][y]=numline[x];
            }
        }
    }
}
Last edited on
Forgot to mention, to pass or return an array pointer, better use a dynamic array.
Returning a static array pointer barely helps because there's no direct assignment operator for it.
Topic archived. No new replies allowed.