我被要求编写一个程序,该程序获取二维数组(矩阵),列数和行数,程序将返回转置矩阵(不使用[ ] [],意思是只使用指针算术)返回二维数组的问题
我写的程序确实转置矩阵,这是没问题的。我的问题是了解如何返回。这里是我的代码:
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