所以我已經通過一些在線問題了解如何設置和填充一個動態的多維數組,我以爲我把它放下了,但由於某種原因,我的函數將不會訪問任何東西(0)列。對於我的測試,我把一個2X2陣列:通過動態分配的矩陣循環
2 2 (part of another function that gives the size of the matrix) 1 2 3 4
當我調試它,我只得到
1 random# from initializing the array 3 random# from initializing the array
我不知道我在做什麼毛病這段代碼,我也是新的C++。
double* matrix_read(const int m, const int n)
{
double **mat = new double*[m];
for (int j = 0; j < m; ++j) {
mat[j] = new double[n];
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
cin >> mat[i][j];
}
}
double debug = mat[2][2];
return *mat;
}
在主函數內部,這是我發送給函數的內容。
int x, y, m, n;
cin >> x;
cin >> y;
double *mat_a = matrix_read(x, y);
cin >> m; //2nd matrix read in
cin >> n;
double *mat_b = matrix_read(m, n);
發表一些更多的代碼 – P0W
@PWW我添加了函數調用以及如何將參數傳遞給函數。 (double debug = mat [2] [2])是我試圖讀取並出錯的調試輸出。 – Motorscooter
您只返回矩陣'return * mat;'的第一行而不是整個矩陣。除了mat [2] [2]',你的2x2數組是無界限的。 – aslg