I can’t seem to figure out what I’m doing wrong.
int main(int argc, char **argv) {
int height = 4, width = 6;
int **map;
map = (int **)(malloc(height * sizeof(int*)));
for(i = 0; i < height; i++) {
map[i] = (int *)(malloc(width * sizeof(int)));
}
fill_map(&map, height, width);
}
void fill_map(int ***map, int height, int width) {
int i, k, character;
for(i = 0; i < height; i++) {
k = 0;
while((character = getchar()) != '\n') {
*map[i][k] = character;
k++;
}
}
}
I get a segfault in fill_map, in the inner while loop, why?
Don’t send &map to the function, change the prototype to receive (int **map) or, use (*map)[i][k]. This is because indirection operator * has lower precedence than [] operator.