Qbasicnews.com

Full Version: ? about UDT's
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Here's another question about possible compiler features. I was wondering if in sometime in the future will if be possible to return UDT's from function w/out using a pointer. Say if I have a library made in another language like C as the following:

MYUDT Func1(int v1, float v2, char* v3);

Will the following work for now in FB?

Declare Function Func1 Lib "udtlib" Alias "Func1" (v1 As Integer, v2 As Single, v3 As String) As Integer

And using MemCpy in crt.dll to get it

Dim a As MYUDT

MemCpy @a, Func1(0, 432.23, "Test), Len(a)

Thanks in advanced
BTW this code doesn't work (based of byval udt example)

type BAR
a as byte
b as byte
c as byte
end type

function foo() As BAR Ptr
dim t as BAR

t.a = 13
t.b = 23
t.c = 34
foo = @t

end Function

Dim d As BAR

d = *foo()

print d.a, d.b, d.c

sleep
Default argument mode is by reference (to be VB/QB compatible), so you must use a BYVAL on every declaration, like:

Declare Function Func1 Lib "udtlib" Alias "Func1" (byval v1 As Integer, byval v2 As Single, byval v3 As String) As Integer

That test won't work because the returned udt is being allocated on stack, it will be undefined when the function returns, so you would have to do: static t as BAR, or allocate t dynamically using ALLOCATE(), see the examples\allocate.bas for more details.
Stupid me. It's a pointer, duh! Ok, I'll try static. But will it be allowed in the future to return UDT types byval?

Function Test() As UDT