title: A queue using structs and dynamic memory allocation
body: <p>I am tasked with making a queue data structure in C, as a linked list. Our lecturer gave us a large amount of code to implement a stack, but we have to adapt it to create a queue. The code our lecturer gave us ends up not compiling and segfaulting at the exact same point as the code I wrote for the queue. I'm very new to structs, malloc and C in general, so there could be something painfully obvious I've overlooked.</p>
  
  <p>Here is the code I am using:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  struct node{
      int data;               //contains the actual data
      struct node *prev;      //pointer to previous node (Closer to front)
      struct node *next;      //pointer to next node (Closer to back)
  };
  
  typedef struct node *Nodepointer;
  
  struct queue{
      Nodepointer front;
      Nodepointer back;
  };
  
  typedef struct queue *Queuepointer;
  
  main(){
      Queuepointer myqueue;       //create a queue called myqueue
      init(myqueue);              //initialise the queue
      Nodepointer new = (Nodepointer)malloc(sizeof(struct node));
      myqueue-&gt;front = new;
  }
  
  int init(Queuepointer q){ 
      q = (Queuepointer)malloc(sizeof(struct queue));
      q-&gt;front = NULL;
      q-&gt;back = NULL;
  }
  </code></pre>
  
  <p>The idea is that the queue struct 'contains' the first and last nodes in a queue, and when a node is created, myqueue is updated. However, I cannot even get to that part (pop and push are written but omitted for brevity). The code is segfaulting at the line</p>
  
  <pre><code>myqueue-&gt;front = new;
  </code></pre>
  
  <p>with the following gdb output:</p>
  
  <pre><code>Program received signal SIGSEGV, Segmentation fault.
  0x08048401 in main () at queue.c:27
  27  myqueue-&gt;front = new;
  </code></pre>
  
  <p>Any idea what I'm doing wrong?</p>