c++ - 将 Matrix 作为指向函数的指针传递

标签 c++ function pointers matrix

在我的项目中,我有这些文件:

functions.h
functions.cc
main.cc

我试图以这种方式将 Matrix 作为指针传递给函数:

ma​​in.cc

// Size -> const short Size = 10;
int mtr1[Size][Size];
matrix_insert((int *)mtr1);

函数.h

void matrix_insert(int *mtr);

函数.cc

void matrix_insert(int *mtr) {
  short i, j;

  for (i = 0; i < Size; i++) {
    for (j = 0; j < Size; j++) {
      std::cin >> *(mtr + i * Size + j);
    }
  }
}

这确实有效,但我不喜欢这种方式...
有没有更好的方法?

谢谢!

编辑: 可以用 vector 模拟矩阵吗?

最佳答案

如果你真的想使用C数组,那么你可以这样做:

ma​​in.cc

int mtr1[Size][Size];
matrix_insert(mtr1);

函数.h

const short Size = 10;
void matrix_insert(int mtr[Size][Size]);

函数.cc

void matrix_insert(int mtr[Size][Size]) {
  short i, j;

  for (i = 0; i < Size; i++) {
    for (j = 0; j < Size; j++) {
      std::cin >> mtr[i][j];
    }
  }
}

工作版本:http://ideone.com/1ik7T9

关于c++ - 将 Matrix 作为指向函数的指针传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13211815/

相关文章:

c++ - 从 double* 转换为 int 会失去精度

c# - 与 C++ 相比,C# 'ref' 关键字的使用

string - Haskell 函数取出最后一次出现的输入字符

function - 在 Go 中使用函数类型

数组中的php函数

C++ 从函数返回指针。值(value)丢失

c++将空指针发送到函数并将其设置为函数而不丢失范围?

c++ - 这个按位异或有什么意义?

C++ 11通过引用函数传递可变数量的对象

c++ - 删除之前删除的内存