C++ C+中动态分配的2D数组EXC#U BAD#U访问错误+;
我在我的程序的早期阶段,现在我只是尝试初始化一个2D数组来保存所有的破折号,但是我一直得到一个ECX\u BAD\u访问错误。我的代码似乎可以使用方形数组(例如:5x5或6x6),但如果我使用10乘5,则会出现错误C++ C+中动态分配的2D数组EXC#U BAD#U访问错误+;,c++,arrays,runtime,runtime-error,C++,Arrays,Runtime,Runtime Error,我在我的程序的早期阶段,现在我只是尝试初始化一个2D数组来保存所有的破折号,但是我一直得到一个ECX\u BAD\u访问错误。我的代码似乎可以使用方形数组(例如:5x5或6x6),但如果我使用10乘5,则会出现错误 void readMatrix(char **twoDarray, int &rows, int &cols) { std::cout << "Enter number of rows for board"; std::cin >> rows
void readMatrix(char **twoDarray, int &rows, int &cols)
{
std::cout << "Enter number of rows for board";
std::cin >> rows;
std::cout << "Enter number of columns for board";
std::cin >> cols;
//dynamic 2D array initialization
twoDarray = new char*[rows];
for(int i = 0; i < cols; ++i)
twoDarray[i] = new char[rows];
//set elements of array to dashes
for(int i = 0; i < rows; ++i)
for(int j = 0; j < cols; ++j){
twoDarray[i][j] = '-';
}
//printing the array
for(int i = 0; i < rows; ++i){
std::cout << " " << std::endl;
for(int j = 0; j < cols; ++j)
std::cout << twoDarray[i][j] << " ";
}
}
void readMatrix(字符**twoDarray,int&rows,int&cols)
{
std::cout>行;
标准:cout>cols;
//动态二维阵列初始化
twoDarray=新字符*[行];
对于(int i=0;i std::cout您的第一个for循环应该从0到#行,而不是#列。此外,在同一循环的主体中,分配列,而不是行