Segmentation fault while using 2D array
I am trying to write a program to solve simple mazes in C but somehow I can't get it to put the maze in a 2D-array, this is my code:
Code:
int **doolhof;
void allocateMemory(int *column, int *row){
int i;
doolhof = malloc((*column)*sizeof(int));
for(i = 0; i < *column; i++){
doolhof[i] = malloc((*row)*sizeof(int));
if(debug) printf("alloceerGeheugen - i = %d\n", i);
if (doolhof[i] == NULL) {
printf("Not enough memory\n");
exit(-1);
}
}
}
void getMaze(int *maxStap, int *column, int *row){
int i, j;
char temp;
scanf("%d %d", row, column);
*maxStap = (*row) * (*column) + 1;
allocateMemory(column, row);
for (i = 0; i < *row; i++){
for (j = 0; j < *column; j++){
scanf("%c", &temp);
switch (temp) {
case '.':
if(j == (*column) - 1 || j == 0 || i == (*row) - 1 || i == 0) doolhof[i][j] = -4;
else doolhof[i][j] = *maxStap;
break;
case '#':
doolhof[i][j] = -1;
break;
case '^':
doolhof[i][j] = -2;
break;
case '%':
doolhof[i][j] = -3;
break;
default:
j--;
break;
}
}
}
/* Filling the center of the maze with zero */
doolhof[*row/2][*column/2] = 0;
}
void readIn(int *types, int *column, int *row, int *maxStep){
int gate, flower, personen, i, lengte;
scanf("%d %d", &gate, &flower);
scanf("%d", &personen);
for(i = 0; i < personen; i++){
scanf("%d", &lengte);
if(lengte > flower) {
if(lengte < gate) {
types[0] = 1;
} else {
types[2] = 1;
}
} else {
if( lengte < gate) {
types[1] = 1;
} else {
types[3] = 1;
}
}
}
getMaze(maxStep, column, row);
}
int main(int argc, char *argv[]){
int n, i, result;
int column = 0, row = 0, maxStep = 0;
int types[3];
scanf("%d", &n);
resetTypes(types);
for (i = 0 ; i < n; i++){
readIn(types, &column, &rij, &maxStep);
}
return 0;
}
n is the number of mazes to be solved.
readIn is supposed to read in all information relevant to solving the maze such as the maximum hight to be able to step through a gate represented by ^ and -2, the minimum size to be able to step over a flowerbed represented by % and -3. Personen is the size of the team, lengte containts the size of each team member. The four types of team members are:
0 these can step over flowerbeds and walk through gates.
1 these can walk through gates.
2 these can step over flowerbeds.
3 these can not step over flowerbeds nor can they walk through gates, they can only walk on the grass, represented by '.'
Get maze actually fills the 2D array with numbers representing the obstacles in the mazes. Places where everybody can walk are represented by column*row+1, this to facilitate the use of Dijkstra's algorithm later on.
Whenever I try to run this program I get a segmentation fault in the function get maze somewhere in the switch while reading in the last lines of the mazes. I can't imagine what goes wrong especially as reading in the first few lines doesn't seem to be a problem.
I compile with gdb.
I hope anyone can help me.