title: Implementing a FIFO queue in C
body: <p>For an embedded application, I am trying to implement a first-in, first-out (FIFO) queue of structs using ANSI C. The most straightforward way to do this seems to be by implementing a linked-list, so that each structure contains a pointer to the next in the queue. Hence I define the struct itself as:</p>
  
  <pre><code>typedef enum { LED_on, LED_off, etc } Action;
  typedef struct Queued_Action QueuedAction;
  
  struct Queued_Action
  {
      Action       action;
      int          value;
      QueuedAction *nextAction;
  };
  </code></pre>
  
  <p>So far so good. If I define pointers to the first and last items in the queue as:</p>
  
  <pre><code>QueuedAction *firstAction;
  QueuedAction *lastAction;
  </code></pre>
  
  <p>...then I'd like to be able to add a new action to the queue by stating (for example):</p>
  
  <pre><code>if (!add_action_to_queue(LED_on, 100, &amp;lastAction))
       printf("Error!\n);
  </code></pre>
  
  <p>...so on return, lastAction would be a pointer to the newly-created last action in the queue. Hence the routine for adding the action to the queue would look like:</p>
  
  <pre><code>int add_action_to_queue(Action newAction, int newValue, QueuedAction **lastAction)
  {
      QueuedAction *newQueuedAction;
  
      // Create a new action in memory
      if ((newQueuedAction = (QueuedAction *)malloc(sizeof(QueuedAction))) == NULL)
          return 0;
  
      // Make the old 'lastAction' point to the new Action, 
      // and the new Action to point to NULL:
      *lastAction -&gt; nextAction = newQueuedAction;
      newQueuedAction -&gt; nextAction = NULL;
      newQueuedAction -&gt; action = newAction;
      newQueuedAction -&gt; value = newValue;
  
      // Designate the new Action as the new lastAction:
      *lastAction = newQueuedAction;
      return 1;
  }
  </code></pre>
  
  <p>All would be fine and dandy, except this code won't compile. The error is at the line saying </p>
  
  <pre><code>*lastAction -&gt; nextAction = newQueuedAction;
  </code></pre>
  
  <p>...where the compiler claims the item to the left of the '->' is not a valid struct. Surely, however, it must be. If in fact I do what ought to be a wholly redundant cast:</p>
  
  <pre><code>fakeAction = (QueuedAction *)(*lastAction);
  fakeAction -&gt; nextAction = newQueuedAction;
  </code></pre>
  
  <p>...then the compiler is quite happy. However, I'm worried that the error message is hinting at something subtle that I may be doing wrong here. So (to come to the point), can anyone tell me why the compiler isn't happy, and whether there is a better way to do what I'm trying to do here.</p>