I am trying to create a dynamic 2D array for a game that is dependent on user input. I currently have a function that is allocating memory shown here:
我正在尝试为一个依赖于用户输入的游戏创建一个动态2D数组。我现在有一个分配内存的函数:
void mallocGrid(int argc, char** argv) {
int i;
char* height;
char* width;
int inputHeight = strtol(argv[3], &height, 10);
int inputWidth = strtol(argv[4], &width, 10);
char** gridArray = malloc(sizeof(char*) * inputHeight);
for (i = 0; i < inputHeight; i++){
gridArray[i] = malloc(sizeof(char) * inputWidth);
}
}
I have another function to popultate this grid array with the '.' character. Here is what I currently have:
我还有另一个函数来填充这个网格数组。'字符。以下是我目前的情况:
void gridInit(int argc, char** argv) {
char gridArray[MAX_COLUMN][MAX_ROW];
int i, j;
char* height;
char* width;
int inputHeight = strtol(argv[3], &height, 10);
int inputWidth = strtol(argv[4], &width, 10);
for(i=0; i < inputWidth; i++) {
for(j=0; j < inputHeight; j++) {
gridArray[i][j]= '.';
printf("%c\n", gridArray[i][j]);
}
fputc('\n', stdout);
}
}
However, when the user inputs a height of 2 and a width of 3, this is what shows up:
但是,当用户输入2的高度和3的宽度时,这就是出现的情况:
.
.
.
.
.
.
but I was expecting to get:
但我希望得到:
..
..
..
Is this the right way to dynamically create a grid that can be overwritten as the game progresses?
这是动态创建网格的正确方法吗?
2 个解决方案
#1
0
there is no relation of 2-D dynamic array & it's output..in what manner you want to get your output is upto you..remove \n from printf("%c\n", gridArray[i][j]); statement and you will get your output as you are expecting.
2-D动态数组与输出之间没有关系。你想以何种方式得到你的产出?从printf删除\n(“%c\n”,gridArray[i][j]);语句,您将得到您所期望的输出。
#2
0
[too long for a comment]
[太长时间评论]
The two definitions of gridArray
in here
这里是gridArray的两个定义。
void mallocGrid(int argc, char** argv) {
...
char** gridArray = malloc(sizeof(char*) * inputHeight);
and here
这里
void gridInit(int argc, char** argv) {
char gridArray[MAX_COLUMN][MAX_ROW];
...
are independent. Each defines its own variable. The have nothing in common but the name. Even the types are different.
是独立的。每个变量都定义了自己的变量。除了名字,没有什么共同之处。即使是类型也不同。
Both variable's definition "disappear" the moment the function is left.
这两个变量的定义“消失”在函数离开的那一刻。
I am trying to create a dynamic 2D array for a game that is dependent on user input.
我正在尝试为一个依赖于用户输入的游戏创建一个动态2D数组。
You do not do this here:
你在这里不做这个:
char gridArray[MAX_COLUMN][MAX_ROW];
#1
0
there is no relation of 2-D dynamic array & it's output..in what manner you want to get your output is upto you..remove \n from printf("%c\n", gridArray[i][j]); statement and you will get your output as you are expecting.
2-D动态数组与输出之间没有关系。你想以何种方式得到你的产出?从printf删除\n(“%c\n”,gridArray[i][j]);语句,您将得到您所期望的输出。
#2
0
[too long for a comment]
[太长时间评论]
The two definitions of gridArray
in here
这里是gridArray的两个定义。
void mallocGrid(int argc, char** argv) {
...
char** gridArray = malloc(sizeof(char*) * inputHeight);
and here
这里
void gridInit(int argc, char** argv) {
char gridArray[MAX_COLUMN][MAX_ROW];
...
are independent. Each defines its own variable. The have nothing in common but the name. Even the types are different.
是独立的。每个变量都定义了自己的变量。除了名字,没有什么共同之处。即使是类型也不同。
Both variable's definition "disappear" the moment the function is left.
这两个变量的定义“消失”在函数离开的那一刻。
I am trying to create a dynamic 2D array for a game that is dependent on user input.
我正在尝试为一个依赖于用户输入的游戏创建一个动态2D数组。
You do not do this here:
你在这里不做这个:
char gridArray[MAX_COLUMN][MAX_ROW];