Tips and Pitfalls

Character Strings

SPP strings are not scalar variables. Their value cannot be changed by an assignment statement. Strings are, in fact, arrays of short integers, with the additional complication of an extra element at the end for the EOS character. It is possible to declare strings with dynamic memory allocation. In fact, is a common practice to use stack memory for temporary string storage.

Arrays of Strings

It is possible to declare an array of strings, but remember that each string element needs its own EOS character. Typically, the strings would be allocated dynamically and referenced in a called procedure, as shown in Example C.11.

The important points to keep in mind are that strings implemented as arrays of chars (shorts), even though they are declared a fixed size, they may not use the entire declared space. A special character value (EOS, implemented as ASCII NUL) is used as the string terminator. Most procedures that require strings also take an argument specifying the string length. This does not mean that the entire declared string will be used, only the maximum possible string size. There are a few important exceptions.

Characters vs. Strings

Note the distinction between single and double quoted characters. Single quotes indicate the ASCII value of a single character and are treated as an int scalar in processed SPP. Double quoted strings are literal strings and may only be specified as actual procedure arguments or the object of a string declaration. Using single quoted characters in place of a char array can cause unexpected problem, for example in:

stridx ('x', string) 
'x' is an int, while stridx() expects a char. Other routines with this problem include ungetc() and putc(). Note that the cast operator char ('x') does not work! It translates into int(120). You should use something like:

char    x_char 
        x_char = 'x' 
        i = stridx (x_char, string) 
Arrays of Strings
Characters vs. Strings

Generated with CERN WebMaker