2

To exemplify what I am having trouble with. I made this simple program, I tried to read a bunch of tutorials and search my trouble in google but I just did not understand.

#include<iostream>
using namespace std;

void noobFunction( int col , int table[][col]){ // compile error
   table[0][0] = 2;
   cout << table[0][0];
}

int main() {
   cout << "Enter the number of rows in the 2D array:  ";
   int row;
   cin >> row;

   cout << "Enter the number of columns in the 2D array:  ";
   int col;
   cin >> col; 

   int table[lin][col];

   noobFunction(col, table); 

   return 0;
}
Olatunde Garuba
  • 1,049
  • 1
  • 16
  • 21
zezin
  • 21
  • 1
  • Try google-searching "dynamic 2D array C++", or take a look at this question: http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new – BrunoLevy Nov 23 '15 at 21:30

1 Answers1

0

Runtime sized arrays are not a C++ standard feature. If you want dynamic 2D arrays use a std::vector<std::vector<int>>:

std::vector<std::vector<int>> table(lin, std::vector<int>(col));;

and define your function as:

void noobFunction(std::vector<std::vector<int>> &table) {
  // ...
}
101010
  • 41,839
  • 11
  • 94
  • 168