I am very new to c and pointers. Each time I thin k I have understood it, there comes a problem that I don't really understand (I spent some time reading c docs but pointers still remain unclear to me) :
typedef struct {
int q[QUEUESIZE+1];
int first;
int last;
int count;
} queue;
enqueue(queue *q, int x)
{
if (q->count >= QUEUESIZE)
printf("Warning: queue overflow enqueue x=%d\n",x);
else {
q->last = (q->last+1) % QUEUESIZE;
q->q[ q->last ] = x;
q->count = q->count + 1;
}
}
I hope my question will not be too opaque but could someone explain the use of pointer in enqueue function? I thought the principle of queuing was to allocate some precise successive memory addresses, but it is not that for sure....
enqueuefunction?lastelement, only first+count. 2) using unsigned types for sizes, counts, and indexes is preferable (you do not want a negative index or size)