String Type and Pointers

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:

Length

(1 byte)

Capacity

(1 byte)

String data

(up to 255 bytes)

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, the 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

Device platforms include a comprehensive list of built-in string-related functions, such as left(), instr(), asc(), chr(), etc.

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:

Tibbo BASIC
Tibbo C
sub on_sys_init
   dim s as string="Tibbo BASIC"
   s=mid(s,7,255) 'The result will be "BASIC" (notice how "B" is character #7)
end sub
void on_sys_init(){
   string s="Tibbo BASIC";
   s=mid(s,7,255);
}

Implicit String Operations

BASIC and C compilers have these implicit string operations:

Tibbo BASIC
Tibbo C
dim s as string="23Z"
dim c as word=s 'The result in c will be "23"
string s="23Z";
word c=s; //The result in c will be "23"

Tibbo BASIC
Tibbo C
dim i as integer=-123
dim s as string=i 'Compiler will select stri()
unsigned long x=12345678;
string s=x; //Compiler will select lstr()


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
Tibbo C
dim s as string(10)
string <10>s;

Pointers to String Types (Tibbo C Only)

As with any other data type, you can create pointers to strings:

Tibbo C
string s="ABC";
string *s_ptr=&ss;
unsigned char *c_ptr=s; //the pointer will be pointing at the first CHARACTER of the string, not the length field!

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 unsigned char *c_ptr and signed 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):

Tibbo C:
unsigned char *c_ptr=(unsigned char *)&ss; //will be pointing at the length field

String Type and Pointers

Working With Strings

Implicit String Operations

Specifying String Capacity

Pointers to String Types (Tibbo C Only)