Getting Started

(lily) #1

Chapter 8: C Pointers and Arrays


myValue; //load myValue and incr pointer
}
controlling

s that pStateFunc is a pointer to a function that takes a char as a
rame shed.

n(char);

dChar;

sendChar = ‘!’;


// Put a byte in the queue
if(queueCount++ < QUEUESIZE)
nextInQueue++ = myValue;//load myValue and incr pointer
else{ // circle the buffer
nextInQueue = myQueue; // reset pointer
queueCount = 0; // reset counter
nextInQueue++ =


// Do some more


// Get the oldest byte from the queue
if(queueCount < QUEUESIZE)
myValue = *NextInQue + 1;
else // we’ve reached the end so circle around
myValue = myQueue[0];


Function Pointers.........................................................................................


Functions are not variables, but we can define pointers to them anyway. The
question is: ‘why would we want to?’ The answer is that sometimes it is
convenient to keep a list of functions that we can choose based on the functions
position in the list. (Uhhh.......)


We can declare a pointer to a function as follows:


char (*pStateFunc)(char);


which say
pa ter and returns a character when fini


If we have another function declared:


char anotherFunctio


We can set the pointer as follows:


pStateFunc = anotherFunction;

Now:
char returnChar, sen

Free download pdf