Note that there are some explanatory texts on larger screens.

plurals
  1. POC linked list why is my list head variable remaining null (new to C)
    text
    copied!<p>i received great help on my other questions in relation to this link list problem. My current problem is now that the head of the list remains null so i cannot actually link any nodes to it or print anything out to the console.</p> <p>The problem is the insert_node function, so when print is called the while loop doesn't execute since head is null. I've got through it through the de bugger and it is definitely null it has the address 0x0.</p> <p>Is this another malloc issue? im not too great on that yet.</p> <p>code:</p> <pre><code> /* * File: main.c * Author: che16 * * Created on 20 November 2013, 08:59 */ #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include "structure.h" /* * */ node* head = NULL; int main(int argc, char** argv) { int no; printf("enter amount of books \n"); scanf("%d", &amp;no); create_books(no); print_list(head); return (EXIT_SUCCESS); } node* create_books(int no_of_books) { char title[50]; char author[30]; unsigned int number; int i; for (i = 0; i &lt; no_of_books; i++) { node* new_node; new_node = (node *) malloc(sizeof (node)); printf("enter book title \n"); scanf("%s", title); printf("enter author name \n"); scanf("%s", author); printf("enter ISDN number \n"); scanf("%10u", &amp;number); strncpy(new_node-&gt;btitle, title, 40); strncpy(new_node-&gt;name, author, 40); new_node-&gt;isbn = number; new_node-&gt;n = NULL; insert_node(head, new_node); } } void insert_node(node* head, node* insert) { printf("insert called \n"); insert-&gt;n = NULL; if (head == NULL) { head = insert; } else { node* curr = head; while (curr-&gt;n != NULL) { curr = curr-&gt;n; } curr-&gt;n = insert; } printf("finished called \n"); } void delete_node(node* head, node * node) { } void print_list(node * head) { while (head) { printf("%s: \"%s\" (%u)\n", head-&gt;btitle, head-&gt;name, head-&gt;isbn); head = head-&gt;n; } } </code></pre> <p><strong>SOLUTION</strong></p> <pre><code>/* * File: main.c * Author: che16 * * Created on 20 November 2013, 08:59 */ #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include "structure.h" /* * */ node** head = NULL; int main(int argc, char** argv) { int no; printf("enter amount of books \n"); scanf("%d", &amp;no); create_books(no); print_list(head); return (EXIT_SUCCESS); } node* create_books(int no_of_books) { char title[50]; char author[30]; unsigned int number; int i; for (i = 0; i &lt; no_of_books; i++) { node* new_node; new_node = (node *) malloc(sizeof (node)); printf("enter book title \n"); scanf("%s", title); printf("enter author name \n"); scanf("%s", author); printf("enter ISDN number \n"); scanf("%10u", &amp;number); strncpy(new_node-&gt;btitle, title, 40); strncpy(new_node-&gt;name, author, 40); new_node-&gt;isbn = number; new_node-&gt;n = NULL; insert_node(&amp;head, new_node); } } void insert_node(node** head, node* insert) { printf("insert called \n"); insert-&gt;n = NULL; if (*head == NULL) { *head = insert; } else { node* curr = *head; while (curr-&gt;n != NULL) { curr = curr-&gt;n; } curr-&gt;n = insert; } printf("finished called \n"); } void delete_node(node* head, node * node) { } void print_list(node * head) { while (head) { printf("%s: \"%s\" (%u)\n", head-&gt;btitle, head-&gt;name, head-&gt;isbn); head = head-&gt;n; } } </code></pre>
 

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