这段代码是否真的检查出数组的边界?检查超出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