A way to decide in code if a string variable is used or a literal string?

Started by trastikata, Oct 01, 2026, 08:47 PM

Previous topic - Next topic

trastikata

Hello all,

I am looking for a way to determine in code if string variable or a literal text has been passed to the procedure which uses some text input.

Because they use different calls "ByRef" vs "BycRef" it would be practical to have a way to decide if "PtrX" vs "cPtrX" rather than making two different procedures and leaving this decision for the user, which might be wrong.


JonW

Hmm quite a tough one,

You can't tell from the address. RAM and flash are separate address spaces with overlapping ranges, so the proc can't know which one a number belongs to.

What about passing the string by value? This costs more ram though


Proc ShowText(pText As String * 32)


If you don't want the copy, you could use two thin wrappers around one worker proc:



Proc ShowRam(ByRef pAddr As Word)
    ShowWorker(pAddr, 0)
EndProc

Proc ShowCode(BycRef pAddr As Dword)
    ShowWorker(pAddr, 1)
EndProc

Proc ShowWorker(pAddr As Dword, pFromCode As Bit)
    Do
        If pFromCode = 1 Then
            bChar = cPtr8(pAddr++)
        Else
            bChar = Ptr8(pAddr++)
        EndIf
        If bChar = 0 Then Break
        HRSOut bChar
    Loop
EndProc


trastikata

Thanks Jon,

Using a string variable is an option of course but it adds to the RAM footprint, so it is something I want to avoid and I left it as my last resort.

I was thinking that maybe there is some trick, that I don't know of, which can be used in this case.

JonW

I don't know of one, the wrapper would work.  Les may know a trick.

▲