Hi, I am trying to brush up on all things new around here and came across the PtrGlobalDefine docs that has the following example.
#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
*PtrVar references the value in the variable.#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.
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.
Just to clarify.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***
Thank you! Things are getting much clearer now.
cheers
bb
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.
Thanks for the background David.