我想要一个数组 int candidates[9][]
,其中第一个维度是已知的 (9),第二个维度取决于执行。
我发现分配数组的方法如下:
int *candidates[9]; /* first allocation at declaration */
for(int i=0;i<9;i++) candidates[i] = new int[6]; /* allocation at execution */
但是当我那样使用它并尝试访问 candidates[i][j]
时,它不起作用。我用函数 fun()
初始化 candidate[i]
,它返回正确大小的 int[],但是 candidate[i][j 的内容]
是错误的。
candidates[0] = fun();
我不明白我哪里错了...谢谢你的帮助:-)
最佳答案
尝试使用 int *candidates[9]
而不是 int candidates[9][]
,它应该可以工作。
关于c++ - 二维数组分配,C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14316296/