Spin: about pointer and structure
laurent974
Posts: 77
I'm struggling to understand address in spin. I want to define a array of structures.
but my code below just output this
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?
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
Also, in the calls to Print_ptrDayWeather you should use @, like:
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.
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
Main.spin