c - 在c中打印二维数组的段错误

标签 c segmentation-fault multidimensional-array

我正在尝试在 C 中分配和打印二维数组,我的方法似乎有效,但在打印数组时出现段错误。有什么想法吗?

#include <stdio.h>
#include<stdlib.h>

#define WALL 1;
#define EMPTY 0;

int rows;
int columns;

int startRow, startCol;

void getPuzzleParams();
void printMaze();
int solve(char** puzzle, int, int);
void findStartAndEnd(char** puzzle);

main() {
getPuzzleParams();

char **puzzle;
puzzle = (char **)malloc(sizeof(char *)*rows);
int y;
for(y = 0; y < rows;y++)
    puzzle[y] = (char *)malloc(sizeof(char)*columns);

FILE *fptr;
char c;
char file_name[20];
int i,j;

printf("Type in the name of the file containing the Field\n");
scanf("%s",file_name);
fptr=fopen(file_name,"r");
for (i=0; i<rows; i++)
    for (j=0; j<columns; j++){
        c=fgetc(fptr); 
        while ( !((c == '1')||(c =='0')) ) c=fgetc(fptr);
        puzzle[i][j]=c;
    }
fclose(fptr);

for (i=0; i<rows; i++) {
    for (j=0; j<columns; j++)  {
        if (j == 0) printf("\n");                
        printf("%c  ",puzzle[i][j]);
    }
}
printf("\n");

printf("print");
//printMaze(puzzle);
printf("find");
findStartAndEnd(puzzle);
printMaze(puzzle);
solve(puzzle, 1, 2);
}

void getPuzzleParams() {
printf("Enter the dimensions of the puzzle which need to be between 5 and 100\n");
printf("Enter the desired number of Rows: ");
scanf("%d", &rows); 
printf("Enter the desired number of Columns: ");
scanf("%d", &columns);
printf("Rows: %d, Columns: %d\n", rows, columns);
if(rows > 100 || rows < 5 || columns > 100 || columns < 5) {
    getPuzzleParams();
}

}

void printMaze(char** puzzle) {
int i,j;
for (i=0; i<rows; i++)
    for (j=0; j<columns; j++)  {
        if (j == 0) printf("\n");                
        printf("%c  ",puzzle[i][j]);
    }
printf("\n");
printf("sdfsdF");
}

void findStartAndEnd(char** puzzle) {
int foundEnterence = 0;
int i;
printf("start");
for(i = 0; i < columns; i++) {
    printf("top row");
    if(puzzle[0][i]=='0') {
        if(!foundEnterence) {
            foundEnterence = 1;
            startRow = 0;
            startCol = i;
            puzzle[0][i] = 'S';
        } else {
            puzzle[0][i] = 'G';
        }
    }
}
for(i = 0; i < rows; i++) {
    if(puzzle[i][rows]=='0') {
        if(!foundEnterence) {
            foundEnterence = 1;
            startRow = i;
            startCol = rows;
            puzzle[i][rows] = 'S';
        } else {
            puzzle[i][rows] = 'G';
        }
    }
}
for(i = columns; i >= 0; i--) {
    if(puzzle[rows][i]=='0') {
        if(!foundEnterence) {
            foundEnterence = 1;
            startRow = rows;
            startCol = i;
            puzzle[rows][i] = 'S';
        } else {
            puzzle[rows][i] = 'G';
        }
    }
}
for(i = rows; i >= 0; i++) {
    if(puzzle[i][0]=='0') {
        if(!foundEnterence) {
            foundEnterence = 1;
            startRow = i;
            startCol = 0;
            puzzle[i][0] = 'S';
        } else {
            puzzle[i][0] = 'G';
        }
    }
}
}

int solve(char** puzzle, int x, int y) {
printf("%c",puzzle[x][y]);

}

可能是malloc使用不当的问题,但我尝试了几种不同的初始化,但没有成功。

编辑:命令行输出为:

Enter the dimensions of the puzzle which need to be between 5 and 100
Enter the desired number of Rows: 12
Enter the desired number of Columns: 10
Rows: 12, Columns: 10
Type in the name of the file containing the Field
maze.txt

1  1  1  1  1  1  1  0  1  1  
1  1  0  0  1  0  0  0  0  1  
1  0  0  0  1  0  0  1  1  1  
1  0  0  0  0  0  0  0  0  1  
1  0  1  0  0  1  0  1  1  0  
1  1  0  0  0  1  0  1  1  1  
1  1  0  1  1  0  0  0  0  1  
1  0  1  0  0  1  0  1  0  1  
1  1  1  0  1  0  1  0  1  1  
1  0  0  1  0  1  0  0  0  1  
1  0  0  0  1  1  1  0  1  1  
1  1  1  1  1  1  1  1  1  1  
Segmentation fault

最佳答案

您在未粘贴的代码中崩溃了。打印完成,没有错误。如您所见,整个迷宫都已打印出来。您没有看到 printfind 因为它们位于您尚未刷新的输出缓冲区中 - 在代码有机会刷新之前您就崩溃了它。

使用好的调试器来查看错误发生的位置。

关于c - 在c中打印二维数组的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12452263/

相关文章:

c - 如何在 linux 内核模块中分配由 1GB HugePages 支持的 DMA 缓冲区?

有人能告诉我段错误的原因吗 - 这个程序中的(核心转储)错误

c - 段错误和 printf 警告

objective-c - 如何在 Objective C 中构建多维对象数组

c++ - 将多个数组合并为一个二维数组

java - 如何根据第二列对二维数组中的第一列进行排序?

python - 如何将带有 char** argv 的 C 函数输入转换为 python?

c - 将字符串数组传递给函数

c - 尝试将节点插入链表时出现段错误

c - 查找 C 中的段错误