Ok my C is a bit rusty but I figured I’d make my next(small) project in C so I could polish back up on it and less than 20 lines in I already have a seg fault.
This is my complete code:
#define ROWS 4
#define COLS 4
char main_map[ROWS][COLS+1]={
"a.bb",
"a.c.",
"adc.",
".dc."};
void print_map(char** map){
int i;
for(i=0;i<ROWS;i++){
puts(map[i]); //segfault here
}
}
int main(){
print_map(main_map); //if I comment out this line it will work.
puts(main_map[3]);
return 0;
}
I am completely confused as to how this is causing a segfault. What is happening when casting from [][] to **!? That is the only warning I get.
rushhour.c:23:3: warning: passing argument 1 of ‘print_map’ from incompatible pointer type rushhour.c:13:7: note: expected ‘char **’ but argument is of type ‘char (*)[5]’
Are [][] and ** really not compatible pointer types? They seem like they are just syntax to me.
A
char[ROWS][COLS+1]cannot be cast into achar**. The input argument ofprint_mapshould beor
The difference being that a
char**means to point to something that can be dereferenced like this:While a
char(*)[n]is a points to a continuous memory region like thisIf you treat a
(char(*)[5])as a(char**)you get garbage: