Page 1 of 1

Passing a Member of Structure

Posted: 22 January 2010, 0:00 AM
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

Posted: 22 January 2010, 6:37 AM
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

Re: Passing a Member of Structure

Posted: 22 January 2010, 8:35 AM
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.