Revision [15463]

This is an old revision of KeyPgZstring made by CountingPine on 2011-10-25 12:05:25.

 

ZSTRING


Standard data type: 8 bit character string

Syntax:
KeyPgDim dim variable KeyPgAs as Zstring * size
KeyPgDim dim variable KeyPgAs as Zstring KeyPgPtr ptr

Description:
A Zstring 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, FreeBASIC avoids any overflow that could occur on assignment, by truncating the contents to a length of size - 1.
A Zstring KeyPgPtr ptr can point to a standard Zstring, also can be used to implement an "user-managed" Zstring, in this case KeyPgAllocate Allocate/KeyPgReallocate Reallocate/KeyPgDeallocate Deallocate 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. KeyPgChr Chr(0)) may never be contained in the text of a Zstring or the rest of the string will be truncated.

In a Zstring, KeyPgLen Len returns the size of the contained string and KeyPgSizeof Sizeof returns the space allocated to the Zstring. KeyPgSizeof Sizeof only works if the size is known by the compiler, i.e. a fixed-size Zstring variable is passed directly, not as a dereferenced pointer or a KeyPgByref Byref function argument.

This type is provided for easy interfacing with C libraries and to also replace the fixed-length strings, that can't be managed through pointers. Any intrinsic string functions like KeyPgLeft Left will work with Zstring's too, plus any string operator.

Examples:
Dim As ZString * 14 str1 => "hello, world"
Print str1
Print Len(str1)    'returns 12, the size of the string it contains
Print SizeOf(str1) 'returns 14, the size of the variable


Dim As ZString Ptr str2
str2 = Allocate( 14 )
*str2 = "hello, world"
Print *str2
Print Len(*str2)      'returns 12, the size of the string it contains
Print SizeOf(*str2)  'returns len(zstring), the size of the variable


Dialect Differences:
Differences from QB:
See also:
Back to Standard Data Types
Back to String Functions
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode