Shop OBEX P1 Docs P2 Docs Learn Events
Strings and things... — Parallax Forums

Strings and things...

I must be having a second day of being as thick as pig poo...

In the attached file I am using graphics.spin to (try and) display some text stored as a string in the variable 'dataGPS'. This which will change, hence the use of the variable. 'dataGPS' is 'byte dataGPS[80]

Displaying the text via a pointer doesn't work...
    
   dataGPS := string("GPRMC")
   gr.text(-60, 0, @dataGPS)

...but via 'String()' directly does.

   gr.text(-60, 0, string("GPRMC"))

Are the two not equivalent?

Thanks,

Hugh

Comments

  • Dave HeinDave Hein Posts: 6,347
    edited 2016-05-13 13:23
    In the statement "gr.text(-60, 0, @dataGPS)" you are passing the address of a string pointer. You want to pass the contents of the string pointer. Eliminate the "@" operator and use "gr.text(-60, 0, dataGPS)" instead.
  • Mike GreenMike Green Posts: 23,101
    edited 2016-05-13 13:25
    They are not equivalent. "dataGPS := string(...)" assigns the address of the string to the first byte of dataGPS (and it won't fit). You probably want:

    bytemove(@dataGPS,string("GPRMC"),6) ' includes the zero byte at the end
  • JonnyMacJonnyMac Posts: 9,105
    edited 2016-05-13 20:24
    If dataGPS is a word or long you can do this (tested with PST):
      dataGPS := string("GPRMC")
      gr.text(-60, 0, dataGPS)
    
    This works because string() provides the address of the embedded string -- this means you don't need to preface that variable with @.

    In my own programs, I preface pointers variables with "p_" so I know that I don't need @ For a general-purpose string pointer, I would do this:
      p_str := string("GPRMC")
      gr.text(-60, 0, p_str)
    
    If you're going to use the same string in more than one place in your code, it would be best to embed the string in a DAT section.
    dat
    
      GPRMC         byte    "GPRMC", 0
    
    Now you would use @.
      gr.text(-60, 0, @GPRMC)
    

  • Perfect.

    Thank you all.

    :-)
Sign In or Register to comment.