我被要求編寫一個程序,該程序獲取二維數組(矩陣),列數和行數,程序將返回轉置矩陣(不使用[ ] [],意思是隻使用指針算術)返回二維數組的問題
我寫的程序確實轉置矩陣,這是沒問題的。我的問題是瞭解如何返回。這裏是我的代碼:
int** transpose_matrix(matrix mat1,int number_of_rows,int number_of_columns)
{
matrix mat2;
int row_index,column_index;
for(row_index=0;row_index<number_of_rows;row_index++)
{
for(column_index=0;column_index<number_of_columns;column_index++)
**(mat2+(column_index*number_of_rows)+row_index)=**(mat1+(row_index*number_of_columns)+column_index);
}
// at this point, mat2 is exactly the transpose of mat1
return mat2;
}
現在這裏是我的問題:我不能返回一個矩陣,我能做的最接近的事是回到矩陣的第一個值的地址,但即使我做到這一點,所有的矩陣的其餘部分將無法使用,只要我退出transpose_matrix函數回到void main ...我怎樣才能返回mat2?
@nhahtdh malloc不棧上分配空間。 – 2013-03-19 20:44:49
@nhahtdh這是一個動態的堆。 – squiguy 2013-03-19 20:50:48
想到堆,但寫了堆棧。 #_# – nhahtdh 2013-03-19 22:30:02