Strings n Things
GrendelT
Posts: 23
Hey gang,
So I'm still a noob with Spin, only been playing with it for a couple weeks now. I'm still fuzzy on how strings work. In so many other languages I can create a string and reference each char within that string just as I would with any other array. It appears that's not the case in Spin.
Consider the following:
I understand that string( ) is a function that stores a literal value and returns the addr of that string.
So pst.str goes to the address it is passed, then walks down the array and "prints" those characters one at a time until the 0-terminator is reached. So, my question is why can I not access each element (char) of foo?
Ok, so perhaps I should create a byte-array of my own. So here's what I came up with. It's ugly and there's probably a more elegant way to do this, but we're learning...
...but there's no output. What gives?
So, how do you read/write to each element of the array? Surely a whole new string copy/creation isn't necessary.
So I'm still a noob with Spin, only been playing with it for a couple weeks now. I'm still fuzzy on how strings work. In so many other languages I can create a string and reference each char within that string just as I would with any other array. It appears that's not the case in Spin.
Consider the following:
obj pst : "Parallax Serial Terminal" var byte foo[10] pub main pst.Start(115200) foo := string("howdy") pst.str(foo)
I understand that string( ) is a function that stores a literal value and returns the addr of that string.
So pst.str goes to the address it is passed, then walks down the array and "prints" those characters one at a time until the 0-terminator is reached. So, my question is why can I not access each element (char) of foo?
Ok, so perhaps I should create a byte-array of my own. So here's what I came up with. It's ugly and there's probably a more elegant way to do this, but we're learning...
obj pst : "Parallax Serial Terminal" var byte foo[10] pub main pst.Start(115200) foo[0] := "d" foo[2] := "o" foo[3] := "o" foo[4] := "d" foo[5] := "y" foo[6] := 0 pst.str(foo)
...but there's no output. What gives?
So, how do you read/write to each element of the array? Surely a whole new string copy/creation isn't necessary.
Comments
EDIT: Also in the first example you are only using the first element of the array foo[], and since it is a byte variable it can only hold an address with a value from 0 to 255. Your program would have failed if it was larger, and "howdy" was stored in a location after 255.
I changed the characters to have 5 unique letters.
I altered that last line to use the @ operator, and I'm still only getting the first letter. The way you explained it was one expected way of doing it, but no go. Anything else wrong here?
Does the datatype of foo need to be long? (tried that too, but to no avail)
I just realized I had a typo.
It is important that a string be a contiguous group of characters. If you see that foo[1] was missing. Got it figured out now.
CORRECTED
You can copy it from a string statement: or much simpler, define the string in a DAT section: