【问题标题】:Is this a valid way to return a two-dimensional array?这是返回二维数组的有效方法吗?
【发布时间】:2015-05-05 13:15:43
【问题描述】:

给定以下C-code:

#include <stdio.h>

int mat1[2][4] = {
    {9, 10, 11, 12},
    {13, 14, 15, 16}
};

int (*(transpose)(int matrix[][4]))[2] {
    static int mat[4][2];
    int i;
    int j;

    printf("I am the function transpose()\nand I'm transposing 2x4 matrices.\n\n");
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 4; j++) {
            mat[j][i] = matrix[i][j];
        }
    }
    return mat;
}

int main() {
    int (*mat_transpose)[2];
    int i;
    int j;
    mat_transpose = transpose(mat1);

    for (j = 0; j < 2; j++) {
        for (i = 0; i < 4; i++) {
            printf("mat_transpose[%d][%d] = %d\n", i, j, mat_transpose[i][j]);
        }
    }
    return 0;
}

函数transpose() 返回一个二维数组(或者更确切地说是指向指针数组的指针,我猜。)。这是实现这一目标的有效方法吗?查看各种 Stackoverflow 问题,似乎没有标准的方法可以做到这一点,而是很多。关于返回二维或多维数组是否有一些标准?

【问题讨论】:

  • int (*(transpose)(int matrix[][4]))[2] 到底是什么?
  • @SouravGhosh 它是一个指向 2 元素数组的指针,该数组本身由 4 个元素组成。
  • 一般建议是避免在 C 中使用二维数组(所以使用arr1d[i*width+j] 而不是arr2d[i][j]....)。你可能想要一些flexible array member
  • @SouravGhosh;它将transpose 声明为一个函数,该函数需要int (*)[2] 类型的参数并返回一个指向int [2] 的指针。顺便说一句,为更好地理解指针的问题 +1。

标签: c arrays multidimensional-array


【解决方案1】:

最好的办法是在调用函数中分配数组,像这样

#include <stdio.h>

void transpose(int rows, int columns,
        int matrix[rows][columns], int mat[columns][rows])
{
    int i;
    int j;

    printf("I am the function transpose()\n");
    printf("And I'm transposing 2x4 matrices.\n\n");
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < columns; j++)
        {
            mat[j][i] = matrix[i][j];
        }
    }
}

int main()
{
    int mat1[2][4] =
    {
        {9, 10, 11, 12},
        {13, 14, 15, 16}
    };
    int mat_transpose[4][2];
    int i;
    int j;

    transpose(2, 4, mat1, mat_transpose);
    for (j = 0; j < 2; j++)
    {
        for (i = 0; i < 4; i++)
        {
            printf("mat_transpose[%d][%d] = %d\n", i, j, mat_transpose[i][j]);
        }
    }
    return 0;
}

当您将 mat1 作为参数传递时,将其设为全局变量绝对没有意义。

【讨论】:

  • int matrix[rows][columns] 无效 C。试试int matrix[/*rows*/][/*columns*/]
  • @PeterSchneider; C99 添加了此功能,是的,您可以将其传递给函数。
  • @PeterSchneider 您提供的链接以此开头ISO C99 中允许使用可变长度自动数组
  • 嗯,我明白了——在 n1570 草案中,6.7.6.2/10 中有这样的例子,示例 4。令人着迷(抬起眉毛)。
  • @iharob 我读过那句话 ;-) -- 认为 VLA 作为参数是 gcc 特定的。
【解决方案2】:

这是返回二维数组的有效方法吗?

是的。返回指向static 局部变量的指针是有效的。我建议的一件事是你可以typedef 那个返回类型

typedef matx[2];  

matx *transpose(int matrix[][4])){ /* Function body */ }

【讨论】:

    【解决方案3】:

    它是有效的,因为它做了它声称要做的事情,但我想你已经知道了。

    我认为没有返回 n 维数组的标准,只有最佳实践。

    关于你的函数,我唯一能说的是它缺乏通用性(你只处理 2*4 矩阵),但也许这不是你的目标。

    【讨论】:

      【解决方案4】:

      你应该有你的功能

          int **transpose(int **matrix, int row, int columns)
          {
             int **mat;
      
             // Malloc you array, copy what you need and return it
             ...
          }
      

      Row and columns是int **matrix的行数和列数

      【讨论】:

        猜你喜欢
        • 2012-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-27
        • 2015-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多