How to use a 2d array in a reference

normally when I want to pull data out of a function without returning, I could just do something like:


1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>

using namespace std;

void Example(int& x, string& name);
.
.
.
.
int main()
.
.
.


That usually does the trick, but for 2D arrays, Im learning that is not the case.

How can I do the same thing as above, but with a 2d array?

Thanks
When you pass an array to a function you actually passing a pointer so all the changes you make to the array inside the function will affect affect the original array you passed in.
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
void Assign_Array(string line, ifstream& in_file,  string Grid[][size])
{

//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 0;






void Get_Info()
{
    string line ,Grid[size][size];
    ifstream in_file;

    in_file.open( file.txt");

    getline(in_file,line);

    Display(line, in_file);
    Assign_Array(line, in_file,Grid);

cout<<endl;
    for ( int t=0; t<=8; t++)
    {
        for(int s=0; s<=8; s++)
        {
            cout<<Grid[t][s];
        }
        cout<<endl;
    }



}

 




could you please tell me what I am doing wrong with this?
You missed a double quote " on line 36
Assign_Array is missing a closing bracket after the commented return 0.

in_file.open (line 36) has a space instead of a second quotation mark, resulting in messing up the rest of the code.
Last edited on
I see, I must have made a mistake when pasting,my issue isn't the code working, its the fact that when I run it, the Array, Grid, shows something completely wiered, but when I show it in the function Assign_Array it runs fine, but as soon as I try to show it in Get_Info, it shows something completely different
Are you sure the Display function is working properly?

Is size > 8?
Topic archived. No new replies allowed.