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

标签 c multidimensional-array parameter-passing

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

这样就够了吗?

void opera_on_matrix(char 矩阵[][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/59429784/

相关文章:

c - setvbuf 的大小参数对于无缓冲流意味着什么?

Javascript - 将可变参数传递给函数

c++ - 如何使用 C 或 C++ 获取目录中的文件列表?

c - 目标文件的多重定义

c - pangram 函数在 c 中不起作用

javascript - 从多维无穷大数组中删除数组元素

objective-c - 如何将未知大小的二维数组作为方法参数传递

c - 查找矩阵 NxN 中的所有峰值

c++ - "std::string"或 "const std::string&"参数? (参数在内部被复制和修改)

c++ - 正确的值作为函数参数,正确用法?