[英]Two Dimensional Arrays with User Input
I'm making a crossword puzzle grid in which the user enters first the dimensions of the grid then each line separated by a space or enter. 我正在制作一个填字游戏拼图网格,在该网格中,用户首先输入网格的尺寸,然后输入由空格或Enter分隔的每一行。 The program takes in each line and puts each letter of the line in a spot on the grid[][].
该程序接受每一行,并将该行的每个字母放在grid [] []上的某个位置。 So far I've only managed to make my grid work for a dimension 4x4 or smaller, if I make it any bigger any letter found in grid[0][i] just spits out garbage.
到目前为止,如果我将网格扩大到grid [0] [i]中发现的任何字母,那么我只能设法使其网格适合4x4或更小的尺寸,只会吐出垃圾。 If I printf the grid after each iteration of the loop its correct, but if I go back and check its not.
如果我在循环的每个迭代之后都打印了网格,那么它是正确的,但是如果我返回并检查了它是否正确。 Anyone got any ideas?
任何人有任何想法吗?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (){
int i, j, row, column;
scanf("%i", &row);
scanf("%i", &column);
char *userInput = malloc (sizeof(char)*column);
char **grid = malloc(sizeof(char)*column);
for(i=0; i<row; i++){
scanf("%s", userInput);
grid[i] = (char*) malloc (row *sizeof(char));
for (j=0; j<column; j++){
grid[i][j] = userInput[j];
}
}
// double check to see if the grid is correct.
printf("%c", grid[0][2]);
return 0;
}
I think you are mixing indices 我认为您在混合指标
column
amount of char*
pointers char*
指针的column
数 row
(when it should be column
) and allocate a row
amount of char
for each index row
进行迭代(当应为column
),并为每个索引分配row
char
char[row]
with a string which has column
length instead that row
column
长度而不是该row
长度的字符串填充该char[row]
Since you are reading column
characters I assume you want a row x column matrix, what you should do is something like 由于您正在读取
column
字符,因此我假设您需要一个行x列矩阵,因此您应该执行的操作类似于
int i, j, row, columnM
char** grid = malloc(row*sizeof(char*));
char* userInput = malloc(column);
for (i = 0; i < row; ++i)
{
grid[i] = malloc(column);
..
memcpy(grid[i], userInput, column);
}
Actually you don't need sizeof(char)
in allocations since a char is guaranteed to be 1 byte by the standard. 实际上,您不需要分配中的
sizeof(char)
,因为标准保证将char保证为1个字节。
Mind that scanf
will write a nul terminator at the end of the string read regardless the size of the buffer. 请注意,无论缓冲区大小如何,
scanf
都会在读取的字符串末尾写入nul终止符。 So you should provide at least column+1
bytes to read to. 因此,您至少应提供
column+1
个字节以供读取。 This won't prevent the user from entering more characters and cause a buffer overflow, check my comment. 这不会阻止用户输入更多字符并导致缓冲区溢出,请查看我的评论。
Your grid malloc is incorrect. 您的网格malloc不正确。 It should be
它应该是
char **grid = malloc(sizeof(char*)*column);
Grid is an array of "pointer to char" not "char". 网格是“指向char的指针”而不是“ char”的数组。 Seeing garbage is the classic symptom of a buffer overrun (or in this case, buffer under-allocation).
看到垃圾是缓冲区溢出(或在这种情况下,缓冲区分配不足)的典型症状。
Also, while not strictly necessary if you are careful, all character buffers should be allocated with an extra byte for the string terminator (zero). 此外,虽然不是绝对必要的 ,如果你是细心,所有的字符缓冲区应该要与为字符串结束(零),一个额外的字节分配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.