Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>r</code> in your code is a pointer to a <code>Node</code> that you allocated dynamically in a segment of the memory called the <em>heap</em>. The <em>heap</em> segment contains all the data for which you allocated space via the malloc-family. The data in that segment in persistant, and that's why you need to free it yourself with <code>free(void *)</code>.</p> <p>Note that <code>r</code> <strong>points</strong> to somewhere in the <code>heap</code> segment where your data exists. The <code>r</code> variable itself is in the <em>stack</em> segment, which contains all the usual variables you declare, like <code>i</code> and <code>j</code>. Since the <code>r</code> variable is declared inside the <code>for</code> loop, <strong>its scope is limited to that loop</strong>. That means that <code>&amp;r</code> will point to somewhere in the <em>stack</em> where the <code>r</code> variable is, but <code>r</code> will get removed at the end of the loop. You can't use <code>&amp;r</code> because <code>r</code> won't exist outside of the loop.</p> <p>You don't need to use <code>Node_t **</code> here. You can just store the address of the allocated memory and then fetch the address in the for loop as a pointer and use it as usual. Maybe the <code>typedef Node_t *Row;</code> isn't helping, you shouldn't use them for that kind of thing. As NPE pointed out, you have too many pointer levels for this.</p> <p>Your .h should look like this : </p> <pre><code>struct node_t{ int vertex; }; struct graph_t{ int noOfVertices; node_t *rowPointer; }; </code></pre> <p>You need to do some minor changes like these :</p> <pre><code>graph-&gt;rowPointer = (node_t *)malloc(noOfVertices * sizeof(node_t)); graph_p *graph = malloc(sizeof(graph_t)); </code></pre> <p>Now you have to use <code>node_t *</code> instead of <code>Row</code>. Just store the address directly like that :</p> <pre><code>node_t *r = malloc(noOfVertices * sizeof(node_t)); graph-&gt;rowPointer[i] = r; </code></pre> <p>Just rewrite some lines to use a <code>node_t *</code> instead of a <code>node_t **</code>.</p> <p><strong>EDIT :</strong> The rest of your header is really bad to be honest. You shouldn't use that much typedefs. And also the structures should not begin with capital letters. Changed the .h.</p> <p><strong>EDIT 2 :</strong> Also the <code>node_t</code> struct here is pointless, since it only contains an <code>int</code>. This is where you want to use a <code>typedef</code>, so instead of making that struct, just do a <code>typedef int node;</code></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. 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