2d Array into function with vary size depend on txt file

Hi everyone,

I'm trying to create a function that manipulates an 2d array read from a text file. So the dimensions vary depends on the file size, which is found in another function.

My question is how to pass this 2d array without stating the size in the parameter.

Here's my code so far:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void create(char** arr, fstream&in, int r, int c)  //create array according to file
{
    in.clear();
    in.seekp(0, ios::beg);
     char cha;

     cout<<r<<" by "<<c<<endl;
     arr=new char*[r];
       for(int i=0; i<r; i++)
        {
           arr[i] = new char[c];
            for(int j=0; j<c; j++)
            {
               in.get(cha);
                    if(cha=='\n') //when get to end of line in file
                    {
                        in.get(cha);//skip to next character
                    }
                    *((*(arr+i))+j)=cha;//stores charater to each cell

                    cout<<*((*(arr+i))+j);//prints out each cell
            }cout<<endl;
        }
}


This function creates the array, which works fine.

but when I write another function that access the array and display it, it crashes. (I know I have display inside the create function, but this is just practice so I know how to write a manipulative function with the array)

1
2
3
4
5
6
7
8
9
10
11
void display(char** arr)
{
    
    for(int i=0; i<row; i++)
        {
            for(int j=0; j<column; j++)
            {
                cout<< arr[i][j];
            }cout<<endl;
        }
}


Thank you for all your help!
Last edited on
Topic archived. No new replies allowed.