Passing Structures by address convention

Just starting out? Need help? Post your questions and find answers here.
devox
User
User
Posts: 32
Joined: Thu Apr 01, 2021 7:25 pm

Passing Structures by address convention

Post by devox »

Hi,

I have noticed that structures can automatically be passed by address if needed without the use of '@'. I'm guessing this here to make the structures easier to work with?

The example in the documentation specifies the @ and it was only by accident that I discovered that it worked the same without. I discovered it when I had a pointer to a root structure with a nested structure inside and I needed the address of the nested structure.

Code: Select all

@*myBar\Foo
I thought the above syntax looked a little strange and found I could simplify to.

Code: Select all

*myBar\Foo
Here is an example showing my experimentations with this convention.

Code: Select all

Structure Foo
  Val.l
EndStructure

Procedure UpdateFoo(*foo.Foo)
  *foo\Val = Random(500)
EndProcedure

Define myFoo.Foo

; The following 2 lines work the same?
;UpdateFoo(myFoo)
UpdateFoo(@myFoo)

Debug "myFoo\Val = " + myFoo\Val

Structure Bar
  Foo.Foo
EndStructure

Define *myBar.Bar = AllocateStructure(Bar)

; The following 2 lines work the same?
UpdateFoo(*myBar\Foo)
;UpdateFoo(@*myBar\Foo)

Debug "*myBar\Foo\Val = " + *myBar\Foo\Val
FreeStructure(*myBar)
I like the simplified look but the downside of it is it is not explicit that the structure is being passed by address. I thought I would check to see if this is valid Purebasic and not likely to break in the future?
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Passing Structures by address convention

Post by mk-soft »

That is correct that it also works with structures without @. But I would always write it with @ to make it more readable.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
devox
User
User
Posts: 32
Joined: Thu Apr 01, 2021 7:25 pm

Re: Passing Structures by address convention

Post by devox »

Thanks @mk-soft. I have a similar feeling as it seems like it makes it more readable rather than something happening which could be hidden. I guess it is added in to make working with structures easier?
Post Reply