c - 如何在 C 函数中传递二维数组(矩阵)?

标签 c multidimensional-array parameter-passing

我也需要这样做来持久化矩阵上的操作。这是否意味着它需要通过引用传递?

这就够了吗?

void operate_on_matrix(char matrix[][20]);

最佳答案

C 并没有真正的多维数组,但是有几种方法可以模拟它们。将此类数组传递给函数的方式取决于用于模拟多维的方式:

1) 使用数组数组。仅当您的数组边界在编译时完全确定,或者您的编译器支持 VLA's 时,才可以使用此方法。 :

#define ROWS 4
#define COLS 5

void func(int array[ROWS][COLS])
{
  int i, j;

  for (i=0; i<ROWS; i++)
  {
    for (j=0; j<COLS; j++)
    {
      array[i][j] = i*j;
    }
  }
}

void func_vla(int rows, int cols, int array[rows][cols])
{
  int i, j;

  for (i=0; i<rows; i++)
  {
    for (j=0; j<cols; j++)
    {
      array[i][j] = i*j;
    }
  }
}

int main()
{
  int x[ROWS][COLS];

  func(x);
  func_vla(ROWS, COLS, x);
}

2) 使用指向(动态分配的)数组的(动态分配的)指针数组。这主要用于数组边界直到运行时才知道的情况。

void func(int** array, int rows, int cols)
{
  int i, j;

  for (i=0; i<rows; i++)
  {
    for (j=0; j<cols; j++)
    {
      array[i][j] = i*j;
    }
  }
}

int main()
{
  int rows, cols, i;
  int **x;

  /* obtain values for rows & cols */

  /* allocate the array */
  x = malloc(rows * sizeof *x);
  for (i=0; i<rows; i++)
  {
    x[i] = malloc(cols * sizeof *x[i]);
  }

  /* use the array */
  func(x, rows, cols);

  /* deallocate the array */
  for (i=0; i<rows; i++)
  {
    free(x[i]);
  }
  free(x);
}

3) 使用一维数组并修复索引。这可以与静态分配(固定大小)和动态分配的数组一起使用:

void func(int* array, int rows, int cols)
{
  int i, j;

  for (i=0; i<rows; i++)
  {
    for (j=0; j<cols; j++)
    {
      array[i*cols+j]=i*j;
    }
  }
}

int main()
{
  int rows, cols;
  int *x;

  /* obtain values for rows & cols */

  /* allocate the array */
  x = malloc(rows * cols * sizeof *x);

  /* use the array */
  func(x, rows, cols);

  /* deallocate the array */
  free(x);
}

4) 使用动态分配的 VLA。与选项 2 相比,它的一个优点是只有一个内存分配;另一个是需要更少的内存,因为不需要指针数组。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

extern void func_vla(int rows, int cols, int array[rows][cols]);
extern void get_rows_cols(int *rows, int *cols);
extern void dump_array(const char *tag, int rows, int cols, int array[rows][cols]);

void func_vla(int rows, int cols, int array[rows][cols])
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            array[i][j] = (i + 1) * (j + 1);
        }
    }
}

int main(void)
{
    int rows, cols;

    get_rows_cols(&rows, &cols);

    int (*array)[cols] = malloc(rows * cols * sizeof(array[0][0]));
    /* error check omitted */

    func_vla(rows, cols, array);
    dump_array("After initialization", rows, cols, array);

    free(array);
    return 0;
}

void dump_array(const char *tag, int rows, int cols, int array[rows][cols])
{
    printf("%s (%dx%d):\n", tag, rows, cols);
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
            printf("%4d", array[i][j]);
        putchar('\n');
    }
}

void get_rows_cols(int *rows, int *cols)
{
    srand(time(0));           // Only acceptable because it is called once
    *rows = 5 + rand() % 10;
    *cols = 3 + rand() % 12;
}

(参见 srand() — why call it only once? 。)

关于c - 如何在 C 函数中传递二维数组(矩阵)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40704345/

相关文章:

将双指针连接到多个指针

arrays - 将多维数组传递给函数

c - 为什么段错误和程序以退出代码 139 结束?

c - 从 ARM 汇编到 C 的函数调用的参数传递约定

rust - 如何概括使用不同类型的键传递给函数的HashMap?

c - MinGW gcc 无法解析系统头文件

c - 不了解 Linux 内核实时调度程序中某些功能的内部结构

c++ - 无符号与有符号整数的性能

javascript - 从对象数组中删除特定属性

使用数字的 Linux Case 语句