Problem with structs and pointers(?) - Mega with QueueArray I'm writing a sketch to run on a Mega that builds up an array of timestamps (using the QueueArray library and DS3231 library). I'm having a problem where a struct I'm defining and then queueing is always referring to the first in the list. The relevant bits of the code looks like this:

#include &lt;QueueArray.h&gt;
#include &lt;DS3231.h&gt;
struct payload_t
{
   unsigned long timestamp;
}

QueueArray &lt;payload_t&gt; dataQueue;
DS3231 clock;
RTCDateTime dt;

void setup() {
   clock.begin();
   clock.setDateTime(__DATE__, __TIME__);
}

void loop() {
    payload_t dataPayload;
    dataPayload.timestamp = getTimestamp();
    dataQueue.enqueue(dataPayload);

    payload_t tempItem = dataQueue.dequeue();

    /**
     * At this stage, the dataPayload.timestamp is correct, 
     * but tempItem.timestamp will always show the value from 
     * the first item in the queue
     */

    sleep(1000);
}

unsigned long getTimestamp() 
{
    RTCDateTime dt = clock.getDateTime();
    return dt.unixtime;
}


As per the comment in the main() loop, the first time a packet is added to the queue, the timestamp matches. For subsequent items, the value retrieved from the queue always gives the timestamp from the first item in the queue.  

I feel this is my C knowledge letting me down with something basic, but can anyone point me in the right direction?
It's not your C knowlede letting you down, it's just that you haven't gotten used yet to the weirdness of the arduino ide/compiler.

You do not need to create a main() function.

You need to create a loop() function.

main() is already defined and calls loop() in a loop.

Check any other sketch and you'll notice that it usually defines setup() and loop(), almost never main().

You must put in the setup function all the code that needs to be executed once (settings and initializations) and in the loop() function whatever needs to be executed iteratively.

Also, leave declarations out of loop() unless you want such variables to have scope (and life) limited to only one round of loop()