문제

This is my Conway's Game of Life C code.

Function newgen checks neighboring cells, all eight of them, even if cell is on the edge of the matrix. How can I change it in a way that won't results in accessing out of bound data, and undefined behavior?

Also I have this error:

1   IntelliSense: a value of type "void *" cannot be assigned to an entity of type "int *"  

The code is:

void copy() {
  int i, j;
  for (i = j = 0; i < HEIGHT; i++) {
    for (; j < WIDTH; j++) {
      gb2[i][j] = gb[i][j];
    }
  }
}

void init() {
  int i, j;
  for (i = 0; i < HEIGHT; i++) {
    gb [i] = malloc(sizeof(int)*WIDTH);
    gb2[i] = malloc(sizeof(int)*WIDTH);
  }
  for (i = 0 ; i < HEIGHT; i++) {
    for (j = 0 ; j < WIDTH; j++) {
      gb [i][j] = 0;
    }
  }
  gb[0][0] = 1;
  gb[0][1] = 1;
  gb[1][0] = 1;
  gb[1][1] = 1;
  copy();
}

... int main() { int i; init(); newgen(); printg(); for (i = 0; i < HEIGHT; i++) { free(gb[i]); free(gb2[i]); } }

올바른 솔루션이 없습니다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top