Page 1 of 1

Passing Structures by address convention

Posted: Sat Dec 02, 2023 9:35 am
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?

Re: Passing Structures by address convention

Posted: Sat Dec 02, 2023 12:18 pm
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.

Re: Passing Structures by address convention

Posted: Sat Dec 02, 2023 12:30 pm
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?