Asterisk in Variable names

Started by badbacchus, August 10, 2013, 08:30:26 PM

Previous topic - Next topic

badbacchus

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

George Vagenas

*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.
Thanks

George

snowsnowsnow

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.

George Vagenas

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***
Thanks

George

badbacchus

Thank you!  Things are getting much clearer now.
cheers
bb

DAG_P6

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.

David A. Gray
You are more important than any technology.

badbacchus

Thanks for the background David.