Forum Index
HomeZBasic Home   Forum RulesForum Rules   Forum FAQForum FAQ   MemberlistMemberlist   UsergroupsUsergroups   RSS FeedRSS Feed
Site SearchSite Search   LinksLinks   DownloadDownload   Digests and SubscriptionsDigests and Subscriptions
ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in   RegisterRegister
Passing a Member of Structure

 
Post new topic   Reply to topic    Forum Index -> ZBasic Language
Author Message
liam.zbasic



Joined: 25 Mar 2008
Posts: 143
Location: Southern California (Blue)

Posted: 22 January 2010, 8:00 AM    Post subject: Passing a Member of Structure Reply with quote

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:
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
Back to top
mikep



Joined: 24 Sep 2005
Posts: 765
Location: Austin, TX

Posted: 22 January 2010, 14:37 PM    Post subject: Reply with quote

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:
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
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR

Posted: 22 January 2010, 16:35 PM    Post subject: Re: Passing a Member of Structure Reply with quote

liam.zbasic wrote:
[I] 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:
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.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Forum Index -> ZBasic Language Time synchro. with the server - Timezone/DST with your computer
Page 1 of 1

 


All content Copyright © 2005-2012 Elba Corp. All Rights Reserved.
Opinions expressed in posts are those of the author and not necessarily those of Elba Corp.
Powered by phpBB © 2001, 2005 phpBB Group