[英]access violation writing to 2d array in c
I'm trying to initialise a 2d array, and for some reason I'm getting an exception whenever I try to enter a value into the array.我正在尝试初始化一个二维数组,由于某种原因,每当我尝试在数组中输入一个值时,我都会遇到异常。
I'm passing it to the function in a struct, if that makes a difference?我将它传递给结构中的 function,如果这有什么不同?
Here's the code:这是代码:
#define board_size 10
typedef struct {
int b[board_size][board_size];
} board;
board dead_state(); // initialise a dead board state
void main() {
board test_board = dead_state();
for (int i = 0; i < board_size - 1; i++) {
for (int j = 0; j < board_size - 1; i++) {
test_board.b[i][j] = 0;
}
}
}
board dead_state() {
// generate the matrix, set all values to 0, return
board deadboard;
for (int i = 0; i < board_size - 1; i++) {
for (int j = 0; j < board_size - 1; i++) {
deadboard.b[i][j] = 0;
}
}
return deadboard;
}
edit: problem solved, thanks to hyde's comment: i am blind and cannot see, apparently.编辑:问题解决了,多亏了海德的评论:显然,我是盲人,看不见。
You have what looks like a typo.你有一个看起来像错字的东西。
for (int i = 0; i < board_size - 1; i++) {
for (int j = 0; j < board_size - 1; i++) {
test_board.b[i][j] = 0;
}
}
Both loops increment i
, which means i
goes out of bounds.两个循环都增加i
,这意味着i
超出范围。 You likely meant for the inner loop to increment j
, but I don't know why you're only incrementing i
and j
up to 8
, instead of 9
.您可能打算让内部循环增加j
,但我不知道为什么您只将i
和j
增加到8
,而不是9
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.