C++ 函数修改数组指针作为输入
我想创建一个函数,它接受一个现有的9x9空整数数组,并插入从文件中获取的值,因此该函数还获取文件名作为输入。但我真的不知道如何正确地做到这一点,即使我已经尝试了很多不同的方法。基本上,我是这样做的:C++ 函数修改数组指针作为输入,c++,arrays,function,pointers,C++,Arrays,Function,Pointers,我想创建一个函数,它接受一个现有的9x9空整数数组,并插入从文件中获取的值,因此该函数还获取文件名作为输入。但我真的不知道如何正确地做到这一点,即使我已经尝试了很多不同的方法。基本上,我是这样做的: int board = new int[9][9] //The empty array int addToArray(int *board, string filename) { /* Code to insert values in each field is tested and w
int board = new int[9][9] //The empty array
int addToArray(int *board, string filename) {
/* Code to insert values in each field is tested and works so
I will just show a quick example. The below is NOT the full code */
int someValue = 5;
board[0][0] = someValue;
/* Return a value depending on whether or not it succeeds. The variable 'succes'
is defined in the full code */
if (succes) {
return 0;
} else {
return -1;
}
}
与实际代码相比,这是一个非常简化的示例,但它是一个整体函数,将指向数组的指针传递到某个函数中,并让该函数修改数组,这是我想要的。有人能告诉我怎么做吗?如果有人读完这个问题,我使用了约翰尼链接中的方法3。为了方便,我复制粘贴了代码并添加了一些注释
// Make an empty, and un-sized, array
int **array;
// Make the arrays 9 long
array = new int *[9];
// For each of the 9 arrays, make a new array of length 9, resulting in a 9x9 array.
for(int i = 0; i < 9; i++)
array[i] = new int[9];
// The function to modify the array
void addToArray(int **a)
{
// Function code. Note that ** is not used when calling array[][] in here.
}
passFunc(array);
它应该是简单的
int **board
在函数参数中
简单的规则是*name=name[],因此数组中的[]越多,参数中的*就越多 你的函数原型不是:int addToArrayint**board,string filename吗?因为你有一个数组?我会在有时间的时候测试它,但我想你们已经了解了一些东西。以前从未使用过2D数组,所以我有点困惑:-你应该使用C++ C++。