Shop OBEX P1 Docs P2 Docs Learn Events
Casting and pointer quirk — Parallax Forums

Casting and pointer quirk

mirrormirror Posts: 322
edited 2007-05-09 05:44 in Propeller 1
It seems that if you have the following program:·
VAR
  long val
  byte v[noparse][[/noparse]2]
  
PUB Start
  v[noparse][[/noparse]0] := $10
  v[noparse][[/noparse]1] := $01
  val := v[noparse][[/noparse]0] + (v[noparse][[/noparse]1] << 8)      'val == $0110

  val := word[noparse][[/noparse]@v[noparse][[/noparse]0]][noparse][[/noparse]0]          'val == $0110
  val := word[noparse][[/noparse]@v][noparse][[/noparse]0]             'val == $0110
  val := word[noparse][[/noparse]v][noparse][[/noparse]0]              'val gets some random value

I thought that the final option was OK - but it's not. I can't figure out where it gets its value from. I thought I'd post this for anyone not quite understanding the behaviour of some code they've written.
·

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-05-09 05:44
    The reason the last doesn't work is that it's not dereferencing a pointer but performing an indirect access. You are using the contents of v as your address (location $06 which is in the header of your program), so you are reading the value at address $06.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
Sign In or Register to comment.