感谢您在这里提供帮助
我想要动态二维尺寸作为字符
int n;
char** stars;
int main() {
cin >> n;
for (int i = 0;i < n;i++) {
stars = new char* [n];
stars[i] = new char[n];
for (int j = 0;j < n;j++) {
stars[i][j] = '*';
}
}
for (int i = 0;i < n;i++) {
for (int j = 0;j < n;j++)
cout << stars[i][j]; //this is where access violation is occured
cout << endl;
}
return 0;
}
而且我还想知道,如果我将 * 放在这个数组上,那么每次插入时都会在内存上生成包含“*”的新字符数据(可能是此代码中的堆栈)?
最佳答案
stars = new char* [n];
在循环中的位置错误,因此您为每个字符串重新分配它,并且在新分配的矩阵中仅分配一个字符串。
关于c++ - C++ 中动态二维数组的访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62621389/