WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: badbacchus on August 10, 2013, 08:30:26 PM

Title: Asterisk in Variable names
Post by: badbacchus on August 10, 2013, 08:30:26 PM
Hi, I am trying to brush up on all things new around here and came across the PtrGlobalDefine docs that has the following example.
Code (winbatch) Select

#DefineFunction CheckGlobalValue()
Ptr_Global_A=PtrGlobal(Global_A)
Ptr_Global_B=PtrGlobal(Global_B)
If *Ptr_Global_A != "Potatoes"
   Message("Error","Global_A is not the expected value")
EndIf
*Ptr_Global_B = 1234
#EndFunction
PtrGlobalDefine( Global_A)
PtrGlobalDefine( Global_B)
Global_A = "Potatoes"
retvalue=CheckGlobalValue()
If Global_B != 1234
   Message("Error","Global_B is not the expected value")
EndIf


So... what purpose does the * (asterisk) have in the variable names eg '*Ptr_Global_B = 1234'?

Cheers
bb
Title: Re: Asterisk in Variable names
Post by: George Vagenas on August 11, 2013, 12:18:55 AM
*PtrVar references the value in the variable.
Code (winbatch) Select
#DefineFunction CheckGlobalValue()
Ptr_Global_A=PtrGlobal(Global_A)
Ptr_Global_B=PtrGlobal(Global_B)
If *Ptr_Global_A != "Potatoes"
   f00 = Ptr_Global_A
   f002 = *Ptr_Global_A
pause(`DEBUG PAUSE`, strCat(`f00 = `, f00, @CRLF, `f002 = `, f002))   ;***DEBUG LINE***
EndIf
*Ptr_Global_B = 1234
#EndFunction
PtrGlobalDefine( Global_A)
PtrGlobalDefine( Global_B)
Global_A = "toes"
retvalue=CheckGlobalValue()
If Global_B != 1234
   Message("Error","Global_B is not the expected value")
EndIf
If you run the modified code I think it will be clear.
Title: Re: Asterisk in Variable names
Post by: snowsnowsnow on August 11, 2013, 02:22:13 AM
The * is an operator - that operates on the value contained in the variable.

It is like the other "unary" operator - the 'minus' sign (also, technically, also the 'plus' sign).

For example:

foo = 42
Message("The negative of foo is:",-foo)

Similarly, the expression *foo produces the value stored at the address contained in foo.
Title: Re: Asterisk in Variable names
Post by: George Vagenas on August 11, 2013, 11:41:42 AM
Just to clarify.
Code (winbatch) Select
f00 = 'food'
f002 = ptrglobaldefine(f00)
; This works.
pause(`Debug Pause`, strCat(`f00 = `, f00, @CRLF, `f002 = `, *f002))   ;***DEBUG LINE***
; This doesn't, because f00 is not a variable pointer.
pause(`Debug Pause`, strCat(`f00 = `, f00, @CRLF, `f00 = `, *f00))   ;***DEBUG LINE***
Title: Re: Asterisk in Variable names
Post by: badbacchus on August 11, 2013, 01:34:09 PM
Thank you!  Things are getting much clearer now.
cheers
bb
Title: Re: Asterisk in Variable names
Post by: DAG_P6 on August 12, 2013, 10:11:42 PM
Like many other aspects of WIL, the asterisk was borrowed from the C programming language, in which the asterisk is, in specific contexts, called the Pointer operator.

For example:


    char   a    // Define a variable named a, that holds a character.
    char* a    // Define a variable named a, that points to the location where a character is stored.


FWIW, pointers are generally considered to be among the most complex, confusing, and poorly understood aspects of the C programming language. I think it's fair to say that pointers are one of the hardest concepts in  the computer science topic of Data Structures. I've worked with several programming languages that support pointers to varying degrees. Pointers have been at the center of some of my most challenging design and debugging problems, whether I was working in WinBatch, C, or Assembly.

Title: Re: Asterisk in Variable names
Post by: badbacchus on August 20, 2013, 06:33:33 PM
Thanks for the background David.