Dynamic struct array in function in C : <p>I'm trying to create adjacency list to represent a graph from a list of edges in file "input.txt". I know the basics of how pointers work but I have problem with dynamic struct array which consists of singly linked lists.</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

struct list {
    int vertex;
    struct list *next;
};

void create_list(int v, struct list* **array);

int main()
{
    int i, v = 5;
    struct list *ptr, **array = (struct list **)malloc(sizeof(struct list *) * v);
    for (i = 0; i &lt; v; i++)
        array[i] = NULL;
    array[i] = NULL;

    create_list(v, &amp;array);

    for(i = 0; i &lt; v; i++) {
        ptr = array[i];
        printf("%d: ", i);
        while(ptr != NULL) {
            printf(" -&gt;%d", ptr-&gt;vertex);
            ptr = ptr-&gt;next;
        }
        printf("\n");
    }
    return 0;
}

void create_list(int v, struct list* **array)
{
    int m, n;
    struct list *ptr, *tmp;
    FILE *luki;
    luki = fopen("input.txt", "r");
    while(fscanf(luki, "%d %d\n", &amp;m, &amp;n) == 2) {
        tmp = (struct lista*)malloc(sizeof(struct list));
        tmp-&gt;vertex = n;
        tmp-&gt;next = NULL;
        if (*array[m] == NULL)      //Here my program crashes when m changes from 0 to 1
            *array[m] = tmp;
        else {
            ptr = array[m];
            while(ptr-&gt;next != NULL)
                ptr = ptr-&gt;next;
            ptr-&gt;next = tmp;
        }
    }
    fclose(luki);
}
</code></pre>

<p>Could you please help me figure out how it should look like?</p>

<p>Also, at first I made the function without using pointer to array:</p>

<pre><code>void create_list(int v, struct list **array)

create_list(v, array);
</code></pre>

<p>And it worked really good when debugging and (I'm using CodeBlocks):</p>

<pre><code>0: 4 -&gt;3 -&gt;1
1: 2
2: 3
3:
4:
</code></pre>

<p>but while running the program normally I got this:</p>

<pre><code>0:
1:
2:
3:
4:
</code></pre>

<p>Why the output while debugging was right if passing array to function create_list was wrong?</p>
