C 返回二维数组时出现问题

C 返回二维数组时出现问题,c,pointers,matrix,return,transpose,C,Pointers,Matrix,Return,Transpose,我被要求编写一个程序来获取二维数组(一个矩阵)、列数和行数,该程序将返回转置矩阵(不使用[][],这意味着只使用指针算术) 我写的程序,确实转换了矩阵,没有问题。我的问题是理解如何返回。这是我的密码: int** transpose_matrix(matrix mat1,int number_of_rows,int number_of_columns) { matrix mat2; int row_index,column_index; for(row_index=0;r

我被要求编写一个程序来获取二维数组(一个矩阵)、列数和行数,该程序将返回转置矩阵(不使用[][],这意味着只使用指针算术)

我写的程序,确实转换了矩阵,没有问题。我的问题是理解如何返回。这是我的密码:

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;
}
int**transpose\u矩阵(矩阵mat1,行的整数,列的整数)
{
矩阵mat2;
int行索引,列索引;

对于(row_index=0;row_index1),二维数组不是双指针

第二,动态分配。如果
matrix
是一种二维数组类型,请编写如下代码:

typedef int matrix[ROWS][COLUMNS];
typedef int (*matrix_ptr)[COLUMNS];

matrix_ptr transpose_matrix(matrix m, int rows, int cols)
{
    matrix_ptr transposed = malloc(sizeof(*transposed) * rows);
    // transpose, then
    return transposed;
}

好的:这里有三件事:

  • 不能返回指向局部变量的指针(它将是垃圾) 返回后,将重新使用它所在的堆栈(内存)
  • 数组衰减到传递时指向第一个元素的指针
  • 指针算法:p+1将p中的地址增加sizeof(*p),因此 p指向下一个元素,而不是下一个字节
  • 代码的简单修复(适用于任何矩阵大小):

    int*转置矩阵(int*mat1,int行数,int列数)
    {
    int*mat2=malloc(行数*列数*大小(int));
    int行索引,列索引;
    
    对于(行索引=0;行_index@nhahtdhmalloc不在堆栈上分配空间。@nhahtdh它是动态的堆。想想堆,但想想写的堆栈。####@qPCR4vir你为什么大喊大叫?不管怎样,我想让+1你,但我想看看“//转置,然后”第一部分。@qPCR4vir抱歉,但这与此无关。OP已经编写了转置代码,他只是想找到一种返回整个矩阵的方法。@qPCR4vir为什么不呢?也许我错了,但是“(转置+i)”是“(int)转置+iCOLUMNS)“??好的,这似乎可行,但我现在有另一个问题,我需要打印转置矩阵,但你已经声明m是指向整数的指针,所以你已经将2d数组变成了1d数组,我如何打印转置矩阵?使用像ptr[rows*column+row]这样难看的索引:-(
        int* transpose_matrix(int *mat1,int number_of_rows,int number_of_columns)
        {
            int *mat2=malloc(number_of_rows*number_of_columns*sizeof(int));
            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;
        }
    
    ...
        print(m,r,c); // I hope you have a print()
        int *t=transpose_matrix(m,r,c);
        print (t,c,r);
    ...
        // use t[max: c-1][max: r-1]
        free(t);
    
    typedef int  Matrix[ROWS][COLUMNS];
    typedef int TMatrix[COLUMNS][ROWS];
    typedef int (*pMatrix)[COLUMNS]; 
    typedef int (*pTMatrix)[ROWS];  
    
    pTMatrix transpose_matrix(Matrix m , int rows, int cols)
    {
        pTMatrix t = malloc(sizeof(*t)*cols);
        for (int r=0; r<rows ; ++r)
        for (int c=0; r<cols ; ++r)
          t[c][r]=m[r][c];
        return t;
    }