Questions about Strings

New to FreeBASIC? Post your questions here.
Post Reply
Löwenherz
Posts: 277
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Questions about Strings

Post by Löwenherz »

hello, I have some simple questions For better understanding about Strings:

where does -> String Data Type comes from in freebasic like String, ZString, WString ?

a) it's c++ based and a string class? or b) in c the char type and creates
an array of characters to make a string? c) an own Data Type from quickbasic?

the freebasic String Functions of 'crt\string.bi' I cannot change the c runtime library Is fixed For using.

thanks, loewenherz

Code: Select all

' c
char greetings[] = "Hello World!";
printf("%s", greetings);

' c++
#include <string>

int main() {
    // char-Array mit 12 Elementen
    char a[] = "Hallo Welt!";

    // char-Array mit 12 Elementen
    std::string z = "Hallo Welt!";
}
freebasic

Code: Select all

' freebasic
' zstring example
Dim As ZString * 1000 buffer
Dim As ZString * 1000 b2

buffer = "1234" + Chr(0) + "5678"

Print Len(buffer), buffer
Print
For x As Integer = 0 To 8
   Print buffer[x],Chr(buffer[x])
Next

b2 = buffer
Print
Print Len(b2), b2
Print
For x As Integer = 0 To 8
   Print b2[x],Chr(b2[x])
Next

Sleep

' string example
Dim As String buffers
Dim As String b2a

buffers = "1234" + Chr(0) + "5678"

Print Len(buffers), buffers
Print
For xa As Integer = 0 To 8
   Print buffers[xa],Chr(buffers[xa])
Next

b2a = buffers
Print
Print Len(b2a), b2a
Print
For xa As Integer = 0 To 8
   Print b2a[xa],Chr(b2a[xa])
Next

Sleep
'

header c++

Code: Select all

<cstring> (string.h)
C Strings
This header file defines several functions to manipulate C strings and arrays.

Functions
Copying:
memcpy	Copy block of memory (function)
memmove	Move block of memory (function)
strcpy	Copy string (function)
strncpy	Copy characters from string (function)

Concatenation:
strcat	Concatenate strings (function)
strncat	Append characters from string (function)

Comparison:
memcmp	Compare two blocks of memory (function)
strcmp	Compare two strings (function)
strcoll	Compare two strings using locale (function)
strncmp	Compare characters of two strings (function)
strxfrm	Transform string using locale (function)

Searching:
memchr	Locate character in block of memory (function)
strchr	Locate first occurrence of character in string (function)
strcspn	Get span until character in string (function)
strpbrk	Locate characters in string (function)
strrchr	Locate last occurrence of character in string (function)
strspn	Get span of character set in string (function)
strstr	Locate substring (function)
strtok	Split string into tokens (function)

Other:
memset	Fill block of memory (function)
strerror	Get pointer to error message string (function)
strlen	Get string length (function)

Macros
NULL	Null pointer (macro)

Types
size_t	Unsigned integral type (type)

' ends
Imortis
Moderator
Posts: 1980
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Questions about Strings

Post by Imortis »

Löwenherz wrote: Jun 16, 2025 15:13 hello, I have some simple questions For better understanding about Strings:

where does -> String Data Type comes from in freebasic like String, ZString, WString ?

a) it's c++ based and a string class? or b) in c the char type and creates
an array of characters to make a string? c) an own Data Type from quickbasic?

the freebasic String Functions of 'crt\string.bi' I cannot change the c runtime library Is fixed For using.
This is what the FB String type is internally, so it is basically B in your example above, but there is more to it than that so it is not 1-to-1.

Code: Select all

typedef struct _FBSTRING {
    char           *data;    /**< pointer to the real string data */
    ssize_t         len;     /**< String length. */
    ssize_t         size;    /**< Size of allocated memory block. */
} FBSTRING;
The 'crt\string.bi' file is not for FB strings. That is the C-Runtime (crt) String functions exposed to FB. So they are for C-style strings, which is basically what zstring is.
Löwenherz
Posts: 277
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Questions about Strings

Post by Löwenherz »

Thank you imortis for clarification..

I Made a simple 'dummy test' for a Zstring Type

Only the Print Output doesn't Work and I am Sure thats Not working with my Idea

Code: Select all

' test demo: Define my ZString type
' my idea for Zstring Ptr with a type definition
'
Type ZStringy
    datas As ZString Ptr
End Type

' function to print a ZString
function PrintZString(zs As ZStringy) As ZString Ptr 'ByRef 
    If zs.datas <> 0 Then
        Print zs.datas
    Else
        Print "ZString is empty"
    End If
    Return @zs
End Function

Dim As ZString*14 myZString="Hello, World"

'PrintZString(cast(*myZString)) ' no

PrintZString(*myZstring) '' error 28 expected pointer

'Print myZstring 'ok but without using function printZstring()
sleep
Imortis
Moderator
Posts: 1980
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Questions about Strings

Post by Imortis »

ZString manual

If you are using a zstring ptr, you need to use ALLOCATE to reserve the memory. If you use ZString * X, you don't need to allocate the memory, hence why the final print statement you have commented is the only one that works.

Can I ask what you are trying to accomplish with the type that contains only a zstring ptr?
Löwenherz
Posts: 277
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Questions about Strings

Post by Löwenherz »

Hello imortis, thanks again For the link I know already This example,
I tried only To make a New 'Type ZString' defintion that's working
like a built-in ZString Data Type in freebasic, I thought it would be possible ;)

Is there also Any typedef struct For _FBZSTRING { ... } FBZSTRING; ?

thanks

Code: Select all

'
typedef struct _FBZSTRING {
    char           *data;    /**< pointer to the real string data */
    ssize_t         len;     /**< String length. */
    ssize_t         size;    /**< Size of allocated memory block. */
} FBSTRING;

Code: Select all

' test demo2: Define my ZString type
' my idea for Zstring Ptr with a type definition
' 
Type ZStringy
    data1 As ZString*64 '? 
    datas As ZString Ptr '?
End Type


Declare Function PrintZString Alias "PrintZString" (ze As Zstringy) As ZString Ptr

' function to print a ZString
function PrintZString(zs As ZStringy) As ZString Ptr 'ByRef 
    If zs.datas <> 0 Then
        Print zs.datas
    Else
        Print "ZString is empty"
    End If
    Return @zs
End Function

'Dim As ZString*14 myZString="Hello, World"

Dim As ZString Ptr myZstring 
myZstring = Allocate( 14 )
*myZstring="Hello, World"
Print *myZstring 
Print Len(*myZstring) '12
Print SizeOf(*myZstring) '1

'PrintZString(cast(*myZString)) ' no

' error next line
PrintZString(@myZstring) '' error 58 type mismatch at parameter 1 (zs)

' Print *myZstring 'ok but without using sub printZstring()
Sleep
fxm
Moderator
Posts: 12575
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Questions about Strings

Post by fxm »

Previously I wrote an example of an Auto-Sized Zstring Object, but the code is not as trivial as you might expect.
Löwenherz
Posts: 277
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Questions about Strings

Post by Löwenherz »

hello fxm, thanks For you link And your example, looks interesting I will study :-)
your 'Type ZstringObject Extends ZString' example looks good.

can you Do me a favour and check my last little code example what's wrong here
With the ZString Pointer? it's just a test nothing more..
I would like To get a result For PrintZString() Function at the End

thanks, loewenherz

Code: Select all

' test demo2: Define my ZString type
' my idea for Zstring Ptr with a type definition
' 
Type ZStringy
    data1 As ZString*64 '? 
    datas As ZString Ptr '?
End Type


Declare Function PrintZString Alias "PrintZString" (ze As Zstringy) As ZString Ptr

' function to print a ZString
function PrintZString(zs As ZStringy) As ZString Ptr 'ByRef 
    If zs.datas <> 0 Then
        Print zs.datas
    Else
        Print "ZString is empty"
    End If
    Return @zs ' pointer problem
End Function

'Dim As ZString*14 myZString="Hello, World"

Dim As ZString Ptr myZstring 
myZstring = Allocate( 14 )
*myZstring="Hello, World"
Print *myZstring 
Print Len(*myZstring) '12
Print SizeOf(*myZstring) '1

' error next line, why ???
PrintZString(@myZstring) '' error 58 type mismatch at parameter 1 (zs)

' Print *myZstring 'ok but without using sub printZstring()
Sleep
fxm
Moderator
Posts: 12575
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Questions about Strings

Post by fxm »

Two unsafe solutions to make this twisted code work:

Code: Select all

' test demo2: Define my ZString type
' my idea for Zstring Ptr with a type definition
' 
Type ZStringy
    data1 As ZString*64 '? 
End Type


Declare Function PrintZString Alias "PrintZString" (ze As Zstringy) As ZString Ptr

' function to print a ZString
function PrintZString(zs As ZStringy) As ZString Ptr 'ByRef 
    If Len(zs.data1) > 0 Then
        Print zs.data1
    Else
        Print "ZString is empty"
    End If
    Return @zs.data1 ' pointer problem
End Function

Dim As ZString Ptr myZstring 
myZstring = Allocate( 14 )
*myZstring="Hello, World"
Print *myZstring 
Print Len(*myZstring) '12
Print SizeOf(*myZstring) '1

' error next line, why ???
PrintZString(*Cptr(ZStringy Ptr, myZstring)) '' error 58 type mismatch at parameter 1 (zs)

' Print *myZstring 'ok but without using sub printZstring()
Sleep
or:

Code: Select all

' test demo2: Define my ZString type
' my idea for Zstring Ptr with a type definition
' 
Type ZStringy
    datas As ZString Ptr '?
End Type


Declare Function PrintZString Alias "PrintZString" (ze As Zstringy) As ZString Ptr

' function to print a ZString
function PrintZString(zs As ZStringy) As ZString Ptr 'ByRef 
    If zs.datas <> 0 Then
        Print *zs.datas
    Else
        Print "ZString is empty"
    End If
    Return zs.datas ' pointer problem
End Function

Dim As ZString Ptr myZstring 
myZstring = Allocate( 14 )
*myZstring="Hello, World"
Print *myZstring 
Print Len(*myZstring) '12
Print SizeOf(*myZstring) '1

' error next line, why ???
PrintZString(*Cptr(ZStringy Ptr, @myZstring)) '' error 58 type mismatch at parameter 1 (zs)

' Print *myZstring 'ok but without using sub printZstring()
Sleep
Post Reply