Passing structures to procedures with @

Found an issue in the documentation ? Please report it here !

Moderator: Documentation Editors

User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Passing structures to procedures with @

Post 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
Last edited by luis on Sat Jan 19, 2013 8:29 pm, edited 1 time in total.
"Have you tried turning it off and on again ?"
A little PureBasic review
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Passing structures to procedures with @

Post by davido »

Luis:

Thank you for explaining that; saved me wasting lots of time.
DE AA EB
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Passing structures to procedures with @

Post 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.
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2071
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Passing structures to procedures with @

Post by Andre »

As this one is a general question, Fred should clarify what he want to have written in the docs... :D
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
Post Reply