Hey, im trying to print what is stored in my 2d dynmic array from a file in my print function. However whenever i try to access information in the array, it crashes the program. maybe the array is not being passed back some how, im not sure, any information would be helpful.

In the function getData(), i can output the array.

Attached is all the program files with a test file "data.txt"

Thank you in advance

void getData(int**,int&,int&);
void print(int**,int,int);

int main()
  int **array;
  int row, column;

  return 0;

void print(int **array, int row, int column)
  for(int y=0; y < row; y++)
      cout << endl;
      for(int x=0; x < column; x++)
          cout << array[y][x] << " ";
  cout << endl;
void getData(int **array, int &row,int &column)
  string fileName;
  ifstream data;  
  cout << "Enter File Name\n";
  getline(cin, fileName);
  if (!data.is_open())
    cout << "Error opening file\n";
      data >> row >> column;
      array = new int*[column]; //Allocates memory for the array
      for (int q = 0; q < column; ++q)
        array[q] = new int[row];
      for(int y=0; y < column; y++)
          for(int x=0; x < row; x++)
              data >> array[y][x];

Recommended Answers

All 3 Replies

When you are passing row and col to get Data, you are derefenrencing it. But they are of type int. And what does " >> " operator do? I've never seen it used out side of cin >> .

[edit] er nvm i didn't read your code right.

Your getData( ) function is being passed the int ** BY VALUE. It can allocate memory to that pointer variable, but that allocation only exists within the scope of the function. The int ** in main( ) is still not allocated any memory.

So, to make life interesting, you must pass the int** by reference, like:

void getData( int** & ,int&,int&);

Also, in getData( ), you are using the row and column variables backwards. Do the allocations and reading loops with row as the outer loop, column as the inner loop, as you do in your display function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.