title: Queue array implementation: dequeue function
body: <p>Stuck on the problem with array queue dequeue function implementation.
  It seems that the function itself is correct, but the problem is as far as I understand the pointer in parameters. Why do I exit with Segmentation fault here? </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdbool.h&gt;
  
  #define CAPACITY 3
  
  typedef struct {
          int head;
          int size;
          int elements[CAPACITY];
          } queue;
  
  bool isempty(queue* q);
  int size(queue* q);
  bool enqueue(queue* q, int element);
  bool dequeue(queue* q, int* element);
  
  
  int main(void) {
  
  queue q;
  
  if(isempty(&amp;q)) {
      printf("Queue is empty\n");
  }
  
  enqueue(&amp;q, 34);
  enqueue(&amp;q, 2);
  enqueue(&amp;q, 4);
  dequeue(&amp;q, &amp;q.head);
  
  if(!isempty(&amp;q)) {
      printf("Queue is not empty\n");
  }
  
  printf("%i\n", q.elements[1]);
  
  }
  
  bool isempty(queue* q) {
      return q-&gt;size == 0;
  }
  
  int size(queue* q) {
      return q-&gt;size;
  }
  
  bool enqueue(queue* q, int element) {
  
      if(q-&gt;size &lt; CAPACITY) {
          q-&gt;elements[(q-&gt;head + q-&gt;size) % CAPACITY] = element;
          q-&gt;size++;
          printf("Enqueue: %d\n", element);
          return true;
      }
  
      return false;
  }
  
  bool dequeue(queue* q, int* element) {
  
      if(q-&gt;size &gt; 0) {
          *element = q-&gt;elements[q-&gt;head];
          q-&gt;head = (q-&gt;head+1) % CAPACITY;
          q-&gt;size--;
  
          return true;
      }
  
      return false;
  }   
  </code></pre>