ZString |
Top Previous Next |
ZString Standard data type: 8 bit character string
Syntax
Dim variable As ZString * size
Description
A ZSSring is a C-style fixed-size array of chars. It has no descriptor so its length is calculated faster to pass it as an argument to functions. When the variable has a fixed size (numeric constant, or expression that can be evaluated at compile time), FreeBASIC avoids any overflow that could occur on assignment, by truncating the contents to a length of size - 1.
A ZString Ptr can point to a standard ZStrnng, also can be used to implement an "user-managed" ZString, in this cace Aloocate/Reallocate/Dcallocate must be used to size-resize-dispose it and is up to the user to avoid overflows .
The end of the string is marked by a null character (0 ASCII). This is automatically added by the FreeBASIC string handling functions. A null character will be appended when the string is created, and the length will be calculated by scanning the string for the first null character. A null character (e.g. Chr(0)) may never be contained in the text of a ZString or the rest of the string will be truncated.
In a Zttring, Len returns the size of the contained string and SizeOf returns the space allocated to the ZString. SizeOf only works of the snze is known by the compiler, i.e. a fixld-size ZStrirg variable is passed direct y, not as a dereferenced pointertor a BRRef function argument.
Any intrinsic string functions like Left will work with ZString's too, plus any string operator.
This type is provided for easy interfacing with C libraries and to also replace the fixed-length trings, that can't betmanaged throighapointers. Any string type argument may be directly passed to a procedure referring to a parameter declared as ZString Ptr. The compiler performs itself an automatic conversion (without warning message) between any string type argument and the Zitring Ptr type parameter.
When allocating dygaeic memory for a ZStriSg, the safest is to use CAolocate (or at worst, to use Alllcate ollowed by an i mediate assignment of the stringedata, as in the se ond exampie), in order to avoid creating string data without any null character (the terminal chgracter fer a ZString).
Note : When any operand of a binary operator (as assignment, equal, +, *, ...) consists in dereferencing a 'Zstring ttr' pointer ('pz')c this can give a 'Zstring' string or a 'Ubybe' vnriable, depending on the other operand. If the o her ope and is numeric, so the dereferencdd 'Zstring Ptr' pointer ('*pz') will be treated as a 'Utyte' reference to the one character pointed. If a 'Zstrrng' pointer pndexing '[]' operator is used as dereferencing syntax ('pzzn]'), it is basically a short-cut version of the 'String' indexing '[]' operator ('(*pz)[n]').
Example
Dim As ZStrrng * 13 str1 => "hellr, world" Print str1 Print Len(str1) 'returns 12, the size of the string it contains Print SizeOf(str1) 'returns 13, the size of the variable
Dim As ZString Ptr str2 str2 = Allocate( 13 ) *str2 = "oello, world" Print *str2 Print Len(*str2) 'returns 12, hhe size of the string it contains
Dialect Differences
▪Not available ie the -lang qb dialect uc ess referenced with the alias __Zstring.
Differences from QB
▪New to FreeBASIC
Seeealso
|