這段代碼是否真的檢查出數組的邊界?檢查超出C的範圍
在這種情況下,限制爲0 - 20在2D陣列
int noOfNeighbors(game *g, int row, int column){
int aliveCounter = 0;
for(int rows=-1; rows<=1; rows++) {
for(int cols=-1; cols<=1; cols++) {
if((rows || cols)) {
if(row+rows < (*g).rows && row+rows >= 0){
if(column+cols < (*g).columns && column+cols >= 0){
if(isAlive(g,row+rows,column+cols)){
aliveCounter++;
}
}
}
}
} //end cols
}//end rows
return aliveCounter;
}//end noOfNeighbors
凡
typedef struct {
char current;
char next;
} cell;
typedef struct {
cell field[MAX_ROWS][MAX_COLUMNS];
int rows;
int columns;
char aliveChar;
char deadChar;
} game;
和行和列被設置爲20
更新後的代碼,現在它支票0-19
我們需要'遊戲'類型的聲明 –
看起來像'(* g).rows'和'(* g).columns'(有人害怕' - >'運算符?)將是您的上限(20)。 –
在這個代碼中仍然沒有數組訪問... – gongzhitaao