Some programming languages have a special string data type. This lets you store things like names and addresses in variables of the string type. C does not do this for you, instead it treats a string as an array of characters. This means that you have to write all your own code to work with strings, rather than letting C take care of everything for you.

In C the convention is that a string is an array of characters which is terminated by an element containing the value 0. In this respect the 0 is called the "terminator". We have already seen that we can use the ASCII character set to represent letters and digits, in a string each element of the string array is the ASCII code for the character at that position.

In your program you give a string as a number of characters enclosed in double quote (") characters.

In C for large computers there is a large library of functions which are provided specially for string manipulation. BoostC doesn't have much string handling built in, but what it does provide is very useful. You can create a constant string, and you can manipulate this as an array in other functions. Consider:

const char * message = "hello" ;

The BoostC compiler will create a string (which is actually an array of course) with the pointer message pointing to the base of it. The string will contain the ASCII codes for "hello" with a 0 at the end. The diagram on the right shows how this works.

Note the word const in front of the variable declaration. This tells C that we will never try to change the contents of this array. The BoostC compiler likes to hear this, because strings like this are held in the Read Only Memory (ROM) on the PICmicro microcontroller, and therefore can't be altered.

We can then use the message array in our program in the usual way. Note that we must remember not to fall off the end of the string, and we do this by looking for the terminator. We can also pass our string into a function:

/* lcd print program */
void lcd_print ( const char * m ) ;

The above is the header for a function which is called lcd_print. It accepts a pointer to a constant string, which it then prints to the LCD panel (we will actually write a function like this in the labs)

This means I can write:

lcd_print ( message ) ;

The variable message is passed as a parameter to the lcd_print function, which can then move along the string printing characters until it reaches the terminator value.

We can also write:

lcd_print ( "Goodbye" ) ;

The compiler will generate a constant string when it sees the "Goodbye" (remember that we need double quotes here) and then pass this into lcd_print to display on the panel.

 


string message, is actually a pointer to the first char 'h'