FBC BUG: passing a '(Z|W)string * N' array to a procedure does not raise an error

General FreeBASIC programming questions.
Post Reply
fxm
Moderator
Posts: 12455
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

FBC BUG: passing a '(Z|W)string * N' array to a procedure does not raise an error

Post by fxm »

Passing a '(Z|W)string * N' array to a procedure is allowed.
But the procedure code does not work because the (Z|W)string size is not passed, and a size of 1 character is taken instead by default.

- Bug example:

Code: Select all

Sub test(z() As Zstring * 10)
    For i As Integer = Lbound(z) To Ubound(z)
        Print z(i)
    Next I
End Sub

Dim As Zstring * 10 z(...) = {"one", "two", "three"}
test(z())

Sleep
  • Code: Select all

    one
    ne
    e
    Conversely, passing a 'String * N' array to a procedure raises an error.
    Passing a '[Z|W]string' array also raises an error.

- If everything worked fine, the compiler should also pass the zstring size to the procedure and do internally what I added in a macro:

Code: Select all

Sub test(z() As Zstring * 10, Byval size As Integer)
    #define _z(n) *(@z(Ubound(z)) - Ubound(z) + (n) * size)
    For i As Integer = Lbound(z) To Ubound(z)
'        Print z(i)
        Print _z(i)
    Next I
End Sub

Dim As Zstring * 10 z(...) = {"one", "two", "three"}
test(z(), Sizeof(z(Lbound(z))))

print 

Sleep

- A workaround might be to pass an array of zstring pointers:

Code: Select all

Sub test(pz() As Zstring Ptr)
    For i As Integer = Lbound(pz) To Ubound(pz)
        Print *pz(i)
    Next I
End Sub

Dim As Zstring Ptr pz(...) = {@"one", @"two", @"three"}
test(pz())

Sleep

- Another workaround might be to define your zstring into a Type and then pass an array of Type elements:

Code: Select all

Type myZstring
    z As zstring * 10
End type

Sub test(mz() As myZstring)
    For i As Integer = Lbound(mz) To Ubound(mz)
        Print mz(i).z
    Next I
End Sub

Dim As myZstring mz(...) = {Type("one"), Type("two"), Type("three")}
test(mz())

Sleep
Last edited by fxm on Jan 06, 2025 12:57, edited 2 times in total.
Reason: Completed bug description.
fxm
Moderator
Posts: 12455
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FBC BUG: passing a '(Z|W)string * N' array to a procedure does not raise an error

Post by fxm »

Post Reply