C 将二维数组传递给函数

标签 c arrays function matrix

我接到了一项作业,要求我编写两个 C 函数:

1 - Read a matrix from the user.
2 - Write a matrix to the console.

未指定矩阵维度,因此我无法使用以下内容:

void matwrite(float m[3][3]) { ... }

有什么办法可以解决这种情况吗?

最佳答案

函数签名应该类似于:

void matwrite(float** m, int w, int h);

然后你可以像这样迭代元素:

for (int i = 0; i < w; ++i) {
  for (int j = 0; j < h; ++j) {
    cout << m[i][j] << " "; // For example
  }
  cout << "\n";
}

您可以使用数组及其维度调用该函数,例如:

float arr[][3] = {
  {1, 2, 3},
  {4, 5, 6},
  {7, 8, 9},
  {1, 2, 3}
};

matwrite(arr, 4, 3);

关于C 将二维数组传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26703635/

相关文章:

C char[] 和 *char

C 'TRUE;' 语句

c - 为数组赋值时发生段错误

arrays - 在Matlab中将矩阵中的元素i,j设置为i^j

javascript - 如何扩充现有函数,使其懒惰地展开其参数?

c++ - 从 RGB 值数组中切出平面(就地)的算法

C++动态对象数组,函数引用错误LNK2019

PHP:数组到可变函数参数值

matlab - 在 matlab 中使用 find()

php - 在 PHP 中的类属性中存储闭包函数