neilsf / XC-BASIC

A compiling BASIC dialect for the Commodore-64
https://xc-basic.net/
MIT License
74 stars 15 forks source link

Address of operator doesn't change value when you reassign a string #117

Closed Thraka closed 4 years ago

Thraka commented 4 years ago

When I reassign a string variable to a new location with let testString$ = testString$ + 1 the string pointer is correct and prints one less character, but the @testString$ operator continues to report the same memory address. I'm assuming that this happens because the string memory was allocated during the compile and then any @ was replaced with the memory position. How would you work around this?

let testString$ = "test str"
let i! = strlen!(testString$)

repeat

    print @testString$
    print testString$
    let testString$ = testString$ + 1

    dec i!

until i! = 0

image

neilsf commented 4 years ago

Hi. In your example, the variable testString$ is a string pointer. It is barely an integer that holds the address of the actual string. Thus @testString$ will not give you the address of the string but the address of the pointer, that is a constant integer. To get the address of the string, try DEEK(@testString$)

neilsf commented 4 years ago

or better and faster CAST(@testString$)

Thraka commented 4 years ago

Indeed! Thanks!