Getting Started

(lily) #1

Chapter 8: C Pointers and Arrays


ometimes

char myStack[STACKSIZE]; // create a data stack
kCount = 0;
unsigned char myValue = 0; // create a byte variable

ntrolling

ack

else
w your stack! - overflow”);

olling

ow itvin the other directon
myValue = *--myStack;

y. A circular buffer is
ore like a game of hot potato where the students form a circle and pass a hot
toto from one to the next and it keeps circulating indefinitely around. The hot
potato in our analogy is actually the pointer to the next address in the queue. For
real students it would probably be a joint or a bottle of cheap alcohol.


#define QUEUESIZE 100


unsigned char myQueue[QUEUESIZE]; // create a queue
unsigned char *nextInQueue; //define a pointer to an unsigned char
char queueCount = 0;
unsigned char myValue = 0; // create a byte variable


NextInQue = myQueue; // set pointer


// Do some controlling


stacks, fifo stands for ‘first in first out’. In control applications it is s
convenient to have fifos, private stacks, to manipulate data.


#define STACKSIZE 100


unsigned
char stac


// Do some co


// push a byte on the st
if (stackCount++ < STACKSIZE) // don’t blow your stack
*myStack++ = myValue;


error(“You almost ble


// Do some more contr


// pull a byte off the stack
if(stackCount-- > 0) //don’t bl


else
error(“You almost blew your stack! - underflow”);


Queues (Circular Buffers)
If stacks are like lunchroom trays, then queues are like the line of students waiting
to get a tray. The last one in line is the last one to get a tra
m
po

Free download pdf