c - 指针矩阵在代码中间被取消引用

标签 c pointers

敲了我1个小时的脑袋,找不到答案:

    int *get_possible_moves(int **board, int *w, int *h, int *x, int *y) {
 int movespossible=0;
 int moveslist[2][8];
 //4
 if(x + 1 <= w-1 && y + 2 <= h - 1) {
  if(board[*(int *)y + 2][*(int *)x + 1] == 0) {
   moveslist[movespossible][0] = *(int *)x + 1;
 breakpoint->    moveslist[movespossible][1] = *(int *)y + 2;
   movespossible++;
  }
 }

在标记为断点的行 -> 我的 **board 被取消引用。任何线索为什么会这样?没有并发线程正在使用内存中的这个地方。另外,我根本没有在这一行使用 **board。

    //3
 if(x + 2 <= w - 1 && y + 1 <= h - 1) {
  if(board[*(int *)y + 1][*(int *)x + 2] == 0) {
   moveslist[movespossible][0] = *(int *)x + 2;
   moveslist[movespossible][1] = *(int *)y + 1;
   movespossible++;
  }
 }

此代码可以完美运行。

提前致谢!

最佳答案

你有:

int moveslist[2][8];

...但您经常引用它:

moveslist[movespossible][0]
moveslist[movespossible][1]

...你不应该这样做吗:

moveslist[0][movespossible]
moveslist[1][movespossible]

?

关于c - 指针矩阵在代码中间被取消引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2146248/

相关文章:

c++ - strcmp 或字符串::比较?

c - 为什么释放动态分配的内存会在这里产生问题?

c - 在 C 中使用 strtok() 的段错误

java - 使用光栅 PTR 打印机在 Java 中打印文件

C++ *new 和 new 之间的区别

C++ 将指针传递给非静态成员函数

c - 了解字符串指针数组

c - 为什么这个销毁函数在单链表中抛出段错误

c++ - BOOST共享指针导致冗余引用删除

c - 数值微分