Page 1 of 1

Passing structures to procedures with @

Posted: Sat Nov 10, 2012 9:20 pm
by luis
Pointers and memory access
HELP wrote:
Addresses of variables

To find the address of a variable in your code, you use the at symbol (@). A common reason for using this is when you want to pass a structured type variable to a procedure. You must pass a pointer to this variable as you cannot pass structured variables directly.

Example

Code: Select all

  Structure astruct
    a.w
    b.l
    c.w
  EndStructure
  
  Procedure SetB(*myptr.astruct)
    *myptr\b = 69
  EndProcedure
  
  Define.astruct myvar
  
  SetB(@myvar)
  
  Debug myvar\b

In reality, if you use

SetB(myvar)

the address of the structure is passed anyway.

At most the use of "@" is a good practice to make your intentions clear, and maybe this is what should be written here.

This page in the help seems to express this as mandatory when it isn't.

Or the compiler is changed (I can hear the screams!) to adhere to the help or the help is more sincere and tell this is not really needed but a suggested practice.

"@" should have been mandatory from the start for other reasons too, I think. Anyway at least mention somewhere the name of a structure it's equivalent to its address.

edit: corrected horrific typo

Re: Passing structures to procedures with @

Posted: Fri Nov 16, 2012 10:32 pm
by davido
Luis:

Thank you for explaining that; saved me wasting lots of time.

Re: Passing structures to procedures with @

Posted: Sat Nov 17, 2012 5:58 pm
by BorisTheOld
luis wrote: At most the use of "@" is a good practice to make your intentions clear, and maybe this is what should be written here.

This page in the help seems to express this as mandatory when it isn't.

Or the compiler is changed (I can hear the screams!) to adhere to the help or the help is more sincere and tell this is not really needed but a suggested practice.

"@" should have been mandatory from the start for other reasons too, I think. Anyway at least mention somewhere the name of a structure its equivalent to its address.
I'm a proponent of clearly written code, so I agree that "@" should always be used, even if it's optional.

Inexperienced programmers often find it difficult to visualize how the underlying code operates. All they see is code on a screen rather than what's going on inside the hardware. So anything that clarifies the true nature of things can only be a benefit.

PB doesn't use the more normal ByRef/ByVal notation to indicate how data is being passed to a procedure, so using "@" makes it more obvious that the data is being passed by reference.

Re: Passing structures to procedures with @

Posted: Fri Mar 06, 2020 9:35 pm
by Andre
As this one is a general question, Fred should clarify what he want to have written in the docs... :D