Page 2 of 2

Re: Pointer to String Constant

Posted: Mon Nov 07, 2011 5:40 am
by Demivec

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

Re: Pointer to String Constant

Posted: Mon Nov 07, 2011 9:48 am
by Shield
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)

Re: Pointer to String Constant

Posted: Thu Dec 28, 2017 3:18 am
by skywalk
Bump :idea:
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

Re: Pointer to String Constant

Posted: Fri Dec 29, 2017 9:31 pm
by Mistrel
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.

Re: Pointer to String Constant

Posted: Fri Dec 29, 2017 11:24 pm
by Sicro

Re: Pointer to String Constant

Posted: Fri Dec 29, 2017 11:39 pm
by walbus
It should work, it's useful