Structure as a procedure parameter

Just starting out? Need help? Post your questions and find answers here.
SeaWolf
User
User
Posts: 12
Joined: Mon Mar 15, 2010 6:06 pm
Location: France

Structure as a procedure parameter

Post by SeaWolf »

Hello,

Here is a simplified example to show the problem I'm having :

Code: Select all

Structure Config
    Colour$
EndStructure

Procedure showSettings(*settings.Config)
    
    MessageRequester("", *settings\Colour$)
    
EndProcedure

Procedure updateSettings(*settings.Config)
    
    *settings\Colour$ = "blue"
    showSettings(@settings)
    
EndProcedure

settings.Config
updateSettings(@settings)
So, showSettings is called from updateSettings, but the MessageRequester shows that *settings\Colour$ is empty...

What is wrong ? Probably something about the pointers, I'm not sure I understand everything about that :oops:
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Structure as a procedure parameter

Post by Shardik »

In procedure updateSettings() simply change

Code: Select all

showSettings(@settings)
to

Code: Select all

showSettings(*settings)
because settings and *settings are different variables. An EnableExplicit at the beginning would have informed you about that... :wink:
SeaWolf
User
User
Posts: 12
Joined: Mon Mar 15, 2010 6:06 pm
Location: France

Re: Structure as a procedure parameter

Post by SeaWolf »

Thank you Shardik, I don't know how I missed that! Now it works nicely :)
Yuri_D
User
User
Posts: 68
Joined: Wed Apr 13, 2016 7:39 am

Re: Structure as a procedure parameter

Post by Yuri_D »

Hi!

Could anybody explain please the difference between Proc( @structure) and Proc( structure) calls for:

Code: Select all

Procedure.i Proc( *struct.structure)
Both are working... Which one is correct?

BRG Yury
User avatar
StarBootics
Addict
Addict
Posts: 1006
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: Structure as a procedure parameter

Post by StarBootics »

Yuri_D wrote:Hi!

Could anybody explain please the difference between Proc( @structure) and Proc( structure) calls for:

Code: Select all

Procedure.i Proc( *struct.structure)
Both are working... Which one is correct?

BRG Yury
This is the good way :

Code: Select all

Structure Config
		Colour$
EndStructure

Procedure showSettings(*settings.Config)
		
		MessageRequester("", *settings\Colour$)
		
EndProcedure

Procedure updateSettings(*settings.Config)
		
		*settings\Colour$ = "blue"
		showSettings(*settings)
		
EndProcedure

settings.Config
updateSettings(settings)
The Stone Age did not end due to a shortage of stones !
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Structure as a procedure parameter

Post by Demivec »

Yuri_D wrote:Could anybody explain please the difference between Proc( @structure) and Proc( structure) calls for:

Code: Select all

Procedure.i Proc( *struct.structure)
Both are working... Which one is correct?
They are both the same.

It is true that a structure used as a parameter currently and in the past versions of PureBasic, will always send its address and not its contents but good code etiquette would probably say that you should explicitly show that you are using the address of the structure (@structure) itself to avoid confusion.

To the contrary I personally prefer to usually leave off the '@' operator in this case. :)
Yuri_D
User
User
Posts: 68
Joined: Wed Apr 13, 2016 7:39 am

Re: Structure as a procedure parameter

Post by Yuri_D »

Hi!

Thank you for the explanation!

BRG
Yury
Post Reply