I was asked to write a program that gets a two dimensional array (a matrix), number of columns,and number of rows, and the program will return the transpose matrix (without using a [][], meaning only using pointer arithmetics)
The program I wrote, does indeed transpose the matrix, it is no problem. My problem is understanding how to return. here's my code:
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;
}
now here's my problem: I can't return a matrix, closest thing I can do is return the address of the first value of the matrix, but even if i do that, all the rest of the matrix will be unusable as soon as i exit transpose_matrix function back into void main...How can I return mat2?