Author Topic: Asterisk in Variable names  (Read 3926 times)

badbacchus

  • Newbie
  • *
  • Posts: 7
Asterisk in Variable names
« 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
#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

  • Full Member
  • ***
  • Posts: 102
Re: Asterisk in Variable names
« Reply #1 on: August 11, 2013, 12:18:55 am »
*PtrVar references the value in the variable.
Code: Winbatch
#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

  • Sr. Member
  • ****
  • Posts: 313
Re: Asterisk in Variable names
« Reply #2 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.

George Vagenas

  • Full Member
  • ***
  • Posts: 102
Re: Asterisk in Variable names
« Reply #3 on: August 11, 2013, 11:41:42 am »
Just to clarify.
Code: Winbatch
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

  • Newbie
  • *
  • Posts: 7
Re: Asterisk in Variable names
« Reply #4 on: August 11, 2013, 01:34:09 pm »
Thank you!  Things are getting much clearer now.
cheers
bb

DAG_P6

  • Full Member
  • ***
  • Posts: 184
  • WinBatch rocks, and so does Wilson WindowWare!
    • WizardWrx
Re: Asterisk in Variable names
« Reply #5 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:

Code: [Select]
    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

  • Newbie
  • *
  • Posts: 7
Re: Asterisk in Variable names
« Reply #6 on: August 20, 2013, 06:33:33 pm »
Thanks for the background David.