Shop OBEX P1 Docs P2 Docs Learn Events
Spin: about pointer and structure — Parallax Forums

Spin: about pointer and structure

I'm struggling to understand address in spin. I want to define a array of structures.

but my code below just output this
Day: Dem
Tmin: 5
TMax: 10
Condition: Conditions pluvieuses
Icon: Rain.gif

Day: Dem
Tmin: 5
TMax: 10
Condition: Conditions pluvieuses
Icon: Rain.gif


it's like ToDayWeather's pointer is not a stand alone copy but just point to the address of We_Tmin
what am i doing wrong?
CON
  _clkmode        = xtal1 + pll16x              'Use crystal * 16
  _xinfreq        = 5_000_000                   '5MHz * 16 = 80 MHz

  CR              = 13

  weatherParamCount = 5

OBJ
  PC            : "Parallax Serial Terminal Extended"

VAR
' weather structure
  long We_Tmin                  ' temperature Min
  long We_Tmax                  ' temperature Max
  long We_Day                   ' pointer to day
  long We_Condition             ' pointer to condition
  long We_Icon                  ' pointer to Icon

' DayWeather ptr
  long ToDayWeather
  long Tomorrow

PUB Main
   PC.Start(115_200)                           ' Start Parallax Serial Terminal
   PC.clear

    InsertDayRec(@ToDayWeather,12,40,string ("Auj"), string("Conditions bonnes"),string("Wind.gif"))
    InsertDayRec(@Tomorrow,5,10,string ("Dem"), string("Conditions pluvieuses"),string("Rain.gif"))
    Print_ptrDayWeather(ToDayWeather)
    Print_ptrDayWeather(Tomorrow)

PUB InsertDayRec(ptrDayWeather, Tmin, Tmax,ptrDay,ptrCondition,ptrIcon)
    We_Tmin      := Tmin
    We_Tmax      := Tmax
    We_Day       := ptrDay
    We_Condition := ptrCondition
    We_Icon      := ptrIcon
    'long[ptrDayWeather] := @We_Tmin
    longmove(long[ptrDayWeather],@We_Tmin,weatherParamCount)

PUB Print_ptrDayWeather (ptrDayWeather)
    PC.str(string(CR,CR,"Day: "))
    PC.str( long[ptrDayWeather][2] )

    PC.str(string(CR,"Tmin: "))
    PC.dec( long[ptrDayWeather][0] )

    PC.str(string(CR,"TMax: "))
    PC.dec( long[ptrDayWeather][1] )

    PC.str(string(CR,"Condition: "))
    PC.str( long[ptrDayWeather][3] )

    PC.str(string(CR,"Icon: "))
    PC.str( long[ptrDayWeather][4] )

Comments

  • ToDayWeather is declared as a long, but in InsertDayRec you're trying to copy 5 longs into its space; similarly with Tomorrow. Try changing their definitions to:
        long ToDayWeather[weatherParamCount]
        long Tomorrow[weatherParamCount]
    

    Also, in the calls to Print_ptrDayWeather you should use @, like:
        Print_ptrDayWeather(@ToDayWeather)
        Print_ptrDayWeather(@Tomorrow)
    
  • Thank you for your answer, but it still doesn't work.
  • Ah, you also have to remove the long[] in the longmove command:
       longmove(ptrDayWeather,@We_Tmin,weatherParamCount)
    

    The reason is that longmove already is dereferencing the pointer internally, so using long[ptrDayWeather] causes a double indirection and makes the data be written to the wrong place in memory.
  • laurent974laurent974 Posts: 77
    edited 2016-04-22 19:15
    oh my god it works. thank you !!

    i have tried so many things, i knew i was close, and was becoming upset to turn around the solution. now i climbed one march on spin comprehension.

    [Update]
    since it's a beginner question, i post here the whole code, that may help others beginners.

    This project contains 2 objects:
    - the first one the structure and the setter are defined
    - The main one which give an array of 3 days of forecast weather.

    Weather.spin
    CON 
      weatherParamCount = 5
    
    VAR
    ' weather structure
      long We_Tmin                  ' temperature Min
      long We_Tmax                  ' temperature Max
      long We_Day                   ' pointer to day
      long We_Condition             ' pointer to condition
      long We_Icon                  ' pointer to Icon
    
    
    PUB InsertDayRec(ptrDayWeather, Tmin, Tmax,ptrDay,ptrCondition,ptrIcon)
        We_Tmin      := Tmin
        We_Tmax      := Tmax
        We_Day       := ptrDay
        We_Condition := ptrCondition
        We_Icon      := ptrIcon
        longmove(ptrDayWeather,@We_Tmin,weatherParamCount)
    

    Main.spin
    CON
      _clkmode        = xtal1 + pll16x              'Use crystal * 16
      _xinfreq        = 5_000_000                   '5MHz * 16 = 80 MHz
    
      CR              = 13
    
    OBJ
    
      PC            : "Parallax Serial Terminal Extended"
      We            : "Weather"
    
    VAR
      
      long Forecast[3]    ' 3 days of forecast weather
                          ' it's an array of pointers to days
    
    ' DayWeather ptrs
    ' they store the structure of weather's fields
      long ToDayWeather[We#weatherParamCount]
      long Tomorrow[We#weatherParamCount]
      long ThirdDay[We#weatherParamCount]
      
    PUB Main
    
       PC.Start(115_200)                           ' Start Parallax Serial Terminal
       PC.clear
    
       We.InsertDayRec(@ToDayWeather,12,40,string ("Auj"), string("Conditions bonnes"),string("Wind.gif"))
       We.InsertDayRec(@Tomorrow,5,10,string ("tom"), string("Conditions pluvieuses"),string("Rain.gif"))
       We.InsertDayRec(@ThirdDay,15,20,string ("3eme"), string("Beau Temps"),string("Sunny.gif"))
    
       Forecast[0]:= @ToDayWeather
       Forecast[1]:= @Tomorrow
       Forecast[2]:= @ThirdDay
    
       Print_ptrDayWeather(Forecast[2])
       Print_ptrDayWeather(Forecast[1])
       Print_ptrDayWeather(Forecast[0])
       
    PUB Print_ptrDayWeather (ptrDayWeather)
        PC.str(string(CR,CR))
        PC.str(string("Day: "))
        PC.str( long[ptrDayWeather][2] )
    
        PC.str(string(CR))
        PC.str(string("Tmin: "))
        PC.dec( long[ptrDayWeather][0] )
    
        PC.str(string(CR))
        PC.str(string("TMax: "))
        PC.dec( long[ptrDayWeather][1] )
    
        PC.str(string(CR))
        PC.str(string("Condition: "))
        PC.str( long[ptrDayWeather][3] )
    
        PC.str(string(CR))
        PC.str(string("Icon: "))
        PC.str( long[ptrDayWeather][4] )
    
Sign In or Register to comment.