插入到c中的动态二维数组
我试图将一个值插入到动态二维字符数组中:
theBoard->_board[row][col] = val;
但是在每次将字符分配给(行,列)时,它也会将其放在(行1,列+8)上。 可能是什么问题?
这是数组的分配:
if ((new = (BoardP) malloc(sizeof(struct Board))) == NULL)
{
reportError(MEM_OUT);
return NULL;
}
if ((new->_board = (char**) malloc(rows * sizeof(char*))) == NULL)
{
reportError(MEM_OUT);
return NULL;
}
int i,j;
for (i=0;i<rows;i++)
{
if ((new->_board[i] = (char*) malloc(cols * sizeof(char))) == NULL)
{
reportError(MEM_OUT);
return NULL;
}
当我插入“X”并打印时:
BoardP b = createNewDefaultBoard();
b->_board[2][0] = 'X';
printBoard(b,0,0);
我得到以下输出:
0 1 2 3 4 5 6 7 8 9
0
1 X
2 X
3
4
5
6
7
8
9
im trying to insert a value to a dynamic 2d array of chars:
theBoard->_board[row][col] = val;
but on each assignment of a char to (row,col), it also puts it on (row-1,col+8).
what could be the problem?
here's the allocation of the array:
if ((new = (BoardP) malloc(sizeof(struct Board))) == NULL)
{
reportError(MEM_OUT);
return NULL;
}
if ((new->_board = (char**) malloc(rows * sizeof(char*))) == NULL)
{
reportError(MEM_OUT);
return NULL;
}
int i,j;
for (i=0;i<rows;i++)
{
if ((new->_board[i] = (char*) malloc(cols * sizeof(char))) == NULL)
{
reportError(MEM_OUT);
return NULL;
}
when i'm inserting an 'X' and printing with:
BoardP b = createNewDefaultBoard();
b->_board[2][0] = 'X';
printBoard(b,0,0);
i'm getting this output:
0 1 2 3 4 5 6 7 8 9
0
1 X
2 X
3
4
5
6
7
8
9
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的看板可能只有 8 列。如果您访问第 8 列以上,您将读取(或写入)内存中的下一行,除了最后一行之外的所有内容都是下一行。
Your board probably only has 8 columns. If you access column 8+ you will be reading (or writing) whatever is next in memory, which for all but the last row is the next row.