Getting Started

(lily) #1

Chapter 8: C Pointers and Arrays


onst char ERROR_YOUFOOBARED[] PROGMEM = "You fouled up beyond
= "Situation normal, all fouled
ess!\r\0";
onst char ERROR_WHERE[] PROGMEM = "Where did you learn to program?\r\0";
= "Read the freaking manual!\r\0";

onst char *ERROR_TBL[] = { ERROR_YOUFOOBARED, ERROR_SNAFUED, \

that begins by outputting the following

uled up beyond repair.

ER array: ‘Enter a ‘. Next we sent the
entially send 0 to 4 in this loop). Then we

Let’s define a set of arrays:


c
repair.\r\0";
const char ERROR_SNAFUED[] PROGMEM
up.\r\0";
const char ERROR_STOPTHEMADNESS[] PROGMEM = "Stop the madn
c
const char ERROR_RTFM[] PROGMEM


And then we define an array of pointers to arrays, initializing it with... pointers to
the arrays:


c
ERROR_STOPTHEMADNESS, ERROR_WHERE, ERROR_RTFM };


Now Let’s specify that we write a program
to HyperTerminal;


Enter a 0 for error message: You fo
Enter a 1 for error message: Situation normal, all fouled up.
Enter a 2 for error message: Stop the madness!
Enter a 3 for error message: Where did you learn to program?
Enter a 4 for error message: Read the freaking manual!

In the software we store these string arrays in addition to the error arrays:


const char ENTER[] PROGMEM = "Enter a ";
const char FOR[] PROGMEM = " for error message: ";
char c = '0';


Then we send the lot to HyperTerminal with the following loop:


char c = '0';
for(int i = 0; i < 5; i++)
{
sendFString(ENTER);
sendChar(c + i);
sendFString(FOR);
sendFString(ERROR_TBL[i]);
}


HyperTerminal first receives the ENT
character ‘0’ + i, (this allows us to sequ

Free download pdf