title: C Queue Char Array
body: <p>I'm trying to create a Queue in C which can take in strings as its value.</p>
  
  <p>I have referenced the code from <a href="http://www.thelearningpoint.net/computer-science/data-structures-queues--with-c-program-source-code" rel="nofollow">http://www.thelearningpoint.net/computer-science/data-structures-queues--with-c-program-source-code</a></p>
  
  <p>I am trying to change the input and output of the functions from int to char array (since i'm storing strings).</p>
  
  <p>However I'm new to C and not able to solve the errors present in 2 parts of the code:</p>
  
  <p>In Enqueue function:
   <strong>strcpy(Q->elements[Q->rear], p);</strong></p>
  
  <p>In Front function: <strong>return Q->elements[Q->front];</strong></p>
  
  <p>The error message given was incompatible type char to char*</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;Windows.h&gt;
  
  /*Queue has five properties.
  
  capacity stands for the maximum number of elements Queue can hold.
    Size stands for the current size of the Queue and elements is the array of elements.
    front is the index of first element (the index at which we remove the element)
    rear is the index of last element (the index at which we insert the element) */
  typedef struct Queue
  {
          int capacity;
          int size;
          int front;
          int rear;
          char **elements;
  }Queue;
  
  /* crateQueue function takes argument the maximum number of elements the Queue can hold, creates
     a Queue according to it and returns a pointer to the Queue. */
  Queue * createQueue(int maxElements)
  {
          /* Create a Queue */
          Queue *Q;
          Q = (Queue *)malloc(sizeof(Queue));
          /* Initialise its properties */
          Q-&gt;elements = (char**)malloc(sizeof(char*)*maxElements);
          Q-&gt;size = 0;
          Q-&gt;capacity = maxElements;
          Q-&gt;front = 0;
          Q-&gt;rear = -1;
          /* Return the pointer */
          return Q;
  }
  
  void Dequeue(Queue *Q)
  {
          if(Q-&gt;size!=0)
          {
                  Q-&gt;size--;
                  Q-&gt;front++;
                  /* As we fill elements in circular fashion */
                  if(Q-&gt;front==Q-&gt;capacity)
                  {
                          Q-&gt;front=0;
                  }
          }
          return;
  }
  
  char* front(Queue *Q)
  {
          if(Q-&gt;size!=0)
          {
                  /* Return the element which is at the front*/
                  return Q-&gt;elements[Q-&gt;front];
          }
          return NULL;
  }
  
  void Enqueue(Queue *Q,char *element)
  {
          //char *p = (char *) malloc(strlen(element)+1);
  
          /* If the Queue is full, we cannot push an element into it as there is no space for it.*/
          if(Q-&gt;size == Q-&gt;capacity)
          {
                  printf("Queue is Full\n");
          }
          else
          {
                  Q-&gt;size++;
                  Q-&gt;rear = Q-&gt;rear + 1;
                  /* As we fill the queue in circular fashion */
                  if(Q-&gt;rear == Q-&gt;capacity)
                  {
                          Q-&gt;rear = 0;
                  }
                  /* Insert the element in its rear side */ 
                  strcpy(Q-&gt;elements[Q-&gt;rear], element);
          }
          return;
  }
  
  int main()
  {
          Queue *Q = createQueue(5);
          Enqueue(Q,"test");  // now runtime fails at this line
          Enqueue(Q,"test");
          Enqueue(Q,"test");
          Enqueue(Q,"test");
          printf("Front element is %s\n",front(Q));
          Enqueue(Q,"test");
          Dequeue(Q);
          Enqueue(Q,"test");
          printf("Front element is %s\n",front(Q));
          Sleep(10000);
  }
  </code></pre>
  
  <p>I'm not sure how to change it to store and print out the string. Help appreciated!</p>