String Type and Pointers
|Top Previous Next|
The String is a special variable type in Tibbo BASIC/C. Do not confuse it with a standard string literal.
In ANSI C, string literals are used for array initialization, i.e. unsigned char a="Hello World!" (by the way, this won't work in Tibbo C!).
The result is not a string at all, but an array initialized to the contents of the string literal. The resulting RAM structure is null-terminated.
Tibbo BASIC and C variables of string type use a a special binary structure to store the string's data:
Because this structure includes the length field, there is no need to null-terminate data and the string can include all byte values from 0 to 255.
Since there is also the capacity field, string-handling code of TiOS can make sure that no string operation results in the overflow situation.
This makes Tibbo BASIC/C strings safe.
Working with strings
When manipulating strings, remember that bytes (characters) in a string are counted from 1. That is, there is no position 0, the leftmost character is character #1:
Implicit string operations
BASIC and C compilers have these implicit string operations:
Specifying string capacity
A standard string occupies 257 bytes, and that's a lot! You can save memory by controlling a string's capacity:
Tibbo BASIC: dim s as string(10). Tibbo C: string <10>s;.
Pointers to string types (Tibbo C only)
As with any other data type, you can create pointers to strings:
You can create a signed char or unsigned char pointer and point it to a string (without &!). The last line in the code example above illustrates this.
As a result, the pointer will be looking at the first character of the string ('A'), not the length field!
In Tibbo C, both unsigned char and signed char types are considered to be "character carriers", so this will work for unsinged char *c_ptr and singed char &c_ptr;
This line, on the contrary, will aim the pointer at the actual beginning of the string's structure in memory (the length field):