A char[5][5]
is not a char **
.
Declare your path
function as one of these (they are equivalent):
void path(char (*adj_mat)[5], int u, int v)
void path(char adj_mat[5][5], int u, int v)
and use it like
path(mat, u, v);
Update: Now to the reason for the segfault.
mat
is a 5-array of 5-arrays of char
, which will "decay" into a pointer to 5-arrays of char
when used as an expression. This means that mat
can be used as if it is a pointer to a 5-array. In general a n-array of type T
will decay into a pointer of type T
(note that T
does not decay!).
So note you use mat
as an expression when you pass it to your function pass()
. In C it is not possible to pass an array as-is to a function, but it can only "receive" pointers.
So what's the difference between a pointer to pointer of char
and a pointer to a 5-array of char
? Their type. And their sizes.
Suppose you have a pointer to a pointer to a char
- let's call it p
and we have a pointer to a 5-array q
. In short: char **p
and char (*q)[5]
.
When you write p[i]
, this will be equivalent to *(p+i)
. Here you have pointer arithmetic. p+i
has the value (which is an adress) p
plus i*sizeof(char *)
because p
points to a char *
.
When you look at q
, q+i
would have the value q
plus i*sizeof(char[5])
.
These will almost always be different!
Update (real answer now): It's even worse in your case as you are "forcing" char (*q)[5]
"to be" a char p**
(via your invalid typecast), so in your call
char temp = *adj_mat[1];
adj_mat[1]
will look at the 2nd row of your matrix an try to interpret its value as a pointer (but it is a 5-array!), so when you then dereference via *adj_mat[1]
you will land somewhere in nirvana - segfault!