Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C Programming for microcontrollers (Joe Pardue, 2005).pdf
Скачиваний:
260
Добавлен:
12.08.2013
Размер:
4.55 Mб
Скачать

Chapter 8: C Pointers and Arrays

// Put a byte in the queue if(queueCount++ < QUEUESIZE)

*nextInQueue++ = myValue;//load myValue and incr pointer

else{ // circle the buffer

// reset pointer

nextInQueue = myQueue;

queueCount = 0;

// reset counter

*nextInQueue++ = myValue; //load myValue and incr pointer

}

//Do some more controlling

//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 says that pStateFunc is a pointer to a function that takes a char as a parameter and returns a character when finished.

If we have another function declared:

char anotherFunction(char);

We can set the pointer as follows:

pStateFunc = anotherFunction;

Now:

char returnChar, sendChar; sendChar = ‘!’;

169

Chapter 8: C Pointers and Arrays

returnChar = anotherFunction(sendChar); returnChar = pStateFunc(sendChar);

both calls work exactly the same.

This may seem about as useful as a bicycle for fish, but you’ll see a good example in our discussion of state machines (oooh, oooh, I can hardly wait), in the meantime, try to hold this in your head until we get there.

Complex Pointer and Array Algorithms

C is an ideal language for solving complex data processing and scientific computing problems. Many a computer scientist has made a living being clever and publishing the results. Which is good for us, because almost any complex problem we will come across has already been solved for us. Whether its sorting a database or doing a really fast Fast Fourier Transform, the algorithm will be published somewhere convenient. Try googling ‘C FFT’ to see what I mean. Even if you have lots of time and enjoy solving puzzles, you aren’t likely to develop a better solution than you can borrow. It’s your call.

I hereby declare further pointer discussion to be ‘advanced’ and beyond the needs of our study. Take a look at the last half of K&R’s chapter on Pointers and Arrays and you’ll thank me.

170