所以到目前为止我有以下代码:
#include <stdio.h>
int foo (int *pointer, int row, int col);
int main() {
int array[3][3] ={ {1,2,3},{4,5,6},{7,8,9}};
int *pointer= array[0];
int row = 2;
int col = 2;
int answer = foo (pointer, row, col);
printf("%d", answer); //prints 5 (which is wrong)
printf("%d", array[2][2]); //prints 9
}
int foo (int *pointer, int row, int col){ //I don't want to use any square brackets here, unless I really have to.
int value;
value = *((int *)(pointer+row)+col);
return value;
}
所以我的主要问题是传递一个 2D 指针,请详细解释,因为我还是编码新手。我不想真正改变我传递的内容(因为我想在 foo(pointer, row, col) 中使用指针而不是 foo (array, row, col)。