Passing a Member of Structure

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
liam.zbasic
Posts: 163
Joined: 24 March 2008, 23:33 PM
Location: Southern California (Blue)

Passing a Member of Structure

Post by liam.zbasic »

Hello,

I defined a Structure with a large number of members. Some of the members include arrays. I defined a separate function to operate on an array, but don't want to pass the entire Structure to preserve speed. Is this possible? The following example fails to compile:

Code: Select all

option base 1

Structure bar 
   Dim i as Integer 
   Dim a(1 to 3) as Integer
End Structure 


Sub Main() 
   Dim b1 as bar 
   Dim i  as Integer 

   i = foo(b1.a) 

   debug.print "i=" & i
End Sub 


Function foo(ByRef b() as Integer) as Integer
  foo = b(1)+b(2)+b(3)
End Function

Appreciate your comments.

Liam
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Post by mikep »

It looks like the compiler does have a problem with this code. The following might be a possible workaround. Note that it is untested as I do have a ZX device with me right now.

Code: Select all

Sub Main()
   Dim b1 as bar
   Dim i as Integer

   i = foo(MemAddressU(b1.a))

   Debug.Print "i=" & i
End Sub

Function foo(ByVal addr as UnsignedInteger) as Integer
  Dim b() as Integer Based addr
  foo = b(1)+b(2)+b(3)
End Function
Mike Perks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Passing a Member of Structure

Post by dkinzer »

liam.zbasic wrote: don't want to pass the entire Structure to preserve speed.
It makes no difference. Passing an array and a structure are both done using a reference behind the scenes, whether ByRef or ByVal. The code below compiles without error.

Code: Select all

option base 1
Structure bar
   Dim i as Integer
   Dim a(1 to 3) as Integer
End Structure

Sub Main()
   Dim b1 as bar
   Dim i  as Integer

   i = foo(b1)
   debug.print "i=" & i
End Sub

Function foo(ByRef b as bar) as Integer
  foo = b.a(1)+b.a(2)+b.a(3)
End Function
liam.zbasic wrote:The following example fails to compile:
This is a known problem that has already been corrected. We are in the final stages of preparing a new release.
- Don Kinzer
Post Reply