Note that there are some explanatory texts on larger screens.

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

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload