Hey guys, I'm currently in an intro to C Programming course at university and we have to write some code for a maze solving algorithm for an input text file. The code below is just the code to read the maze input and then print it out with each symbol doubled along the row. Just ignore all the constants and function prototypes that aren't used, since I cut off the rest of the program to make it simpler. This part of the program itself compiles fine and prints out the right output but as soon as I take the comment marks off of the 2D array in the first structure, I get a segmentation fault. This is making me wonder if it is possible to have a 2D array within a lower-level structure? If it is, what is the proper way of coding it? I'd appreciate any comments. Sorry if my coding isn't up to par, I'm still pretty new at this.
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
#define NOGO '#'
#define PASS '.'
#define NEXTCELL '\n'
#define REACHABLE '+'
#define UNREACHABLE '-'
#define NOCOST 0
#define EMPTYCOST -1
#define UNDERLINE "======="
typedef struct {
char type;
char reach;
int exitcol, exitrow;
int startcol, startrow;
/* int costs[MAXSIZE-1][MAXSIZE-1]; */
} mazecell_t;
typedef struct {
int nrows;
int ncolumns;
int nentrances, entcount;
int nexits, exitcount;
int XP, YP; /* position placeholders */
int hassoln;
int costcount;
int ncostarrays, costarraycount;
mazecell_t maze[MAXSIZE-1][MAXSIZE-1];
mazecell_t list[MAXSIZE-1];
} maze_t;
void read_maze(maze_t *M);
void print_stage_one(maze_t *M);
void maze_prep(maze_t *M);
void assign_reachability(maze_t *M);
void print_stage_two(maze_t *M);
int
main(int argc, char *argv[]) {
maze_t M;
read_maze(&M);
print_stage_one(&M);
return 0;
}
void
read_maze(maze_t *M){
int i, j, temp;
for(i=0; i<(MAXSIZE-1); i++){
/* no maze or end of maze */
if ((temp=getchar())==EOF){
break;
}
else {
j=0;
/*fill maze rows*/
while (temp!=NEXTCELL){
if(temp==NOGO){
M->maze[i][j].type = temp;
}
if(temp==PASS){
M->maze[i][j].type = temp;
}
j++;
temp=getchar();
}
}
}
M->ncolumns = j;
M->nrows = i;
return;
}
void
print_stage_one(maze_t *M){
int x, y, i, j;
x = M->ncolumns;
y = M->nrows;
printf("Stage 1\n%s\n", UNDERLINE);
printf("maze has %d rows and %d columns\n", y, x);
for(i=0; i<y; i++){
for(j=0; j<x; j++){
if((M->maze[i][j].type)==NOGO){
printf("%c%c", NOGO, NOGO);
}
if((M->maze[i][j].type)==PASS){
printf("%c%c", PASS, PASS);
}
}
printf("\n");
}
return;
}