Shop OBEX P1 Docs P2 Docs Learn Events
Code is working, but is it failsafe? — Parallax Forums

Code is working, but is it failsafe?

Patrick1abPatrick1ab Posts: 136
edited 2010-06-15 20:35 in Propeller 1
Hi everyone!

I've always got some difficulties when I try to work with pointers, arrays, etc.
This time even the SPIN examples in the Propeller manual couldn't help me anymore.

The following code is working, but I'm not sure if it is allowed the way I did it.
Are these last three lines legal operations?

VAR

  word index, port
  long ipaddr, uri

DAT

  station1    byte      213, 251, 166, 36
              word      8050
              byte      "/",0   'CineMix

  station2    byte      94, 23, 193, 30
              word      80
              byte      "/",0    '1onAir Maxxhits

  stations    long      @station1, @station2


PUB Main

  index:=0      ' station1 selected

  ipaddr:=@byte[noparse][[/noparse]@@stations[noparse][[/noparse]index]]                   ' pointer to the ip address of the selected station
  port:=word[noparse][[/noparse]@@stations[noparse][[/noparse]index]+4]                    ' port of the selected station (value)
  uri:=@byte[noparse][[/noparse]@@stations[noparse][[/noparse]index]+6]                    ' pointer to the uri of the selected station

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2010-06-15 09:51
    Why do you need the @byte[noparse]/noparse? Try to read it in natural language:

    give me the address of the first byte of a byte array starting at given address, which means that the part inside of the square brackets is already the address you want.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-06-15 16:24
    is_it_failsafe.png

    'Sorry, couldn't resist! smile.gif

    -Phil
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-06-15 16:34
    Your code should work OK.· It would be a bit more efficient if you wrote it as

      ipaddr := @@stations[noparse][[/noparse]index]            ' pointer to the ip address of the selected station
      port := word[noparse][[/noparse]ipaddr + 4]               ' port of the selected station (value)
      uri := ipaddr + 6                      ' pointer to the uri of the selected station
    
    
  • Patrick1abPatrick1ab Posts: 136
    edited 2010-06-15 20:35
    Thanks for your replies. I made the changes you suggested and it just works fine.


    @Phil: Nice picture smilewinkgrin.gif
Sign In or Register to comment.