当我尝试将字符存储在2D Malloc数组中时,它说“分离故障(核心倾倒)”
我正在尝试创建一个简单的蛇游戏。这是其中的第一部分,根据用户的命令行输入,使用“ ”显示地图的边界。但是,当我尝试创建malloc数组并储存''时,它会崩溃,说“分割故障(核心倾倒)”。请帮助我找出此错误并修复我的代码。我是C编程的新手。
#include<stdio.h>
#include<stdlib.h>
void initializeSnake(char** pArray, int pRows, int pColumns, int pSnakeLength);
void print(char** pArray, int pRows, int pColumns);
int main(int argc, char* argv[])
{
int rows=atoi(argv[1]);
int columns=atoi(argv[2]);
int snakeLength=atoi(argv[3]);
char** mapArray;
mapArray=(char**)malloc(rows*sizeof(char*));
for(int i=0;i<rows;i++)
mapArray[i]=(char*)malloc(columns*sizeof(char*));
initializeSnake(mapArray, rows, columns, snakeLength);
print(mapArray,rows,columns);
printf("Rows: %s\n",argv[1]);
printf("Columns: %s\n",argv[2]);
printf("Snake Length: %s\n",argv[3]);
return 0;
}
void initializeSnake(char** pArray, int pRows, int pColumns, int pSnakeLength)
{
for(int m=0;m<pRows+2;m++)
{
for(int n=0;n<pColumns+2;n++)
{
if(m==0 || m==pRows+1)
pArray[m][n]=42;
else if(n==0 || n==pColumns+1)
pArray[m][n]=42;
else if(m!=1)
pArray[m][n]=' ';
if(m==1)
{
if(n==0)
pArray[m][n]='#';
else if(n>0 && n<pSnakeLength)
pArray[m][n]='-';
else if(n==pSnakeLength)
pArray[m][n]='>';
else if(n<pColumns)
pArray[m][n]=' ';
}
}
}
}
void print(char** pArray, int pRows, int pColumns)
{
for(int m=0;m<pRows;m++)
{
for(int n=0;n<pColumns;n++)
{
printf("%c",pArray[m][n]);
}
printf("\n");
}
}
I am trying to create a simple snake game. This is first part of it, to display the borders of the map using '' according to user's command line input. However, when I try to create a malloc array and store '' accordingly, it crashes saying "Segmentation fault (core dumped)". Please help me figure out this error and fix my code. I am new to C programming.
#include<stdio.h>
#include<stdlib.h>
void initializeSnake(char** pArray, int pRows, int pColumns, int pSnakeLength);
void print(char** pArray, int pRows, int pColumns);
int main(int argc, char* argv[])
{
int rows=atoi(argv[1]);
int columns=atoi(argv[2]);
int snakeLength=atoi(argv[3]);
char** mapArray;
mapArray=(char**)malloc(rows*sizeof(char*));
for(int i=0;i<rows;i++)
mapArray[i]=(char*)malloc(columns*sizeof(char*));
initializeSnake(mapArray, rows, columns, snakeLength);
print(mapArray,rows,columns);
printf("Rows: %s\n",argv[1]);
printf("Columns: %s\n",argv[2]);
printf("Snake Length: %s\n",argv[3]);
return 0;
}
void initializeSnake(char** pArray, int pRows, int pColumns, int pSnakeLength)
{
for(int m=0;m<pRows+2;m++)
{
for(int n=0;n<pColumns+2;n++)
{
if(m==0 || m==pRows+1)
pArray[m][n]=42;
else if(n==0 || n==pColumns+1)
pArray[m][n]=42;
else if(m!=1)
pArray[m][n]=' ';
if(m==1)
{
if(n==0)
pArray[m][n]='#';
else if(n>0 && n<pSnakeLength)
pArray[m][n]='-';
else if(n==pSnakeLength)
pArray[m][n]='>';
else if(n<pColumns)
pArray[m][n]=' ';
}
}
}
}
void print(char** pArray, int pRows, int pColumns)
{
for(int m=0;m<pRows;m++)
{
for(int n=0;n<pColumns;n++)
{
printf("%c",pArray[m][n]);
}
printf("\n");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您故意将您分配给
您创建的尺寸行的数组之外 * cols * cols
通过此信息将其传递给初始化,
然后超越该功能的界限,
最终您尝试访问比创建的2列,而行也是如此。我不知道该+2的意图,因此无法提出解决方案。但是,正如您发现的那样,您无法做自己当前正在做的事情。
You deliberately go outside the array you allocated
You create with size rows * cols
Pass this info to initializeSnake
Then go beyond the bounds in that function
You end up trying to access 2 more columns than you created, same for rows. I do not know the intent of that +2 so cannot propose a solution. But, as you have discovered, you cannot do what you are currently doing.