Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Demivec
Addict
Posts: 4090 Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA
Post
by Demivec » Mon Nov 07, 2011 5:40 am
Code: Select all
#String1 = "Constant String X"
#String2 = "Constant String X"
#String3 = "Constant String 3"
Procedure test(*StringConstant)
Debug Str(*StringConstant) + ": " + PeekS(*StringConstant)
EndProcedure
Define myString.s = "Variable String"
test(@myString)
test(@"Literal String")
test(?string1)
test(?string2)
test(?string3)
DataSection
String1: Data.s #String1
String2: Data.s #String2
String3: Data.s #String3
EndDataSection
@Edit: shortened example code
Last edited by
Demivec on Mon Nov 07, 2011 2:22 pm, edited 1 time in total.
Shield
Addict
Posts: 1021 Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:
Post
by Shield » Mon Nov 07, 2011 9:48 am
Well if you're going to do something complicated like this, you might as well just go with:
Code: Select all
Macro MyConst
"Hello World"
EndMacro
#MyConst = MyConst
Procedure Pointer(*pointer)
Debug PeekS(*pointer)
ProcedureReturn #True
EndProcedure
Debug #MyConst
Debug MyConst
Pointer(@MyConst)
skywalk
Addict
Posts: 3995 Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA
Post
by skywalk » Thu Dec 28, 2017 3:18 am
Bump
myStringProcedure(@#MYConst$) would have made string library changes much simpler, instead of:
Code: Select all
;- Pointer to a string constant. --> @#MYCONST$
Prototype.i PtrToStr(aString.p-unicode)
Procedure.i PtrToStr(aString.i)
ProcedureReturn aString
EndProcedure
Global atS.PtrToStr = @PtrToStr()
CompilerIf 1
Procedure SF_ShowPtrContents(*p)
; Work with ByRef string. Not its copy.
Debug PeekS(*p)
EndProcedure
#String1 = "StrConst 1"
String2$ = "StrVar 2"
SF_ShowPtrContents(atS(#String1)) ;<-- Annoying for String Constants.
SF_ShowPtrContents(@String2$)
SF_ShowPtrContents(@"Literal String")
CompilerEndIf
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Mistrel
Addict
Posts: 3415 Joined: Sat Jun 30, 2007 8:04 pm
Post
by Mistrel » Fri Dec 29, 2017 9:31 pm
Blood wrote: Why would you need a pointer to a constant though?
It saves memory and processing power by being able to reuse the pointer to a string without having to allocate memory and perform and assignment each time you want to use it somewhere. For example, error messages.
Sicro
Enthusiast
Posts: 538 Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:
Post
by Sicro » Fri Dec 29, 2017 11:24 pm
walbus
Addict
Posts: 929 Joined: Sat Mar 02, 2013 9:17 am
Post
by walbus » Fri Dec 29, 2017 11:39 pm
It should work, it's useful