Arrays or Structures?
jjames
Posts: 2,908
I'm going to guess that this thread may eventually get thrown into the tips and tricks section, but I'd thought I'd start here.
Before I was introduced to the real power of arrays, I used individual variables for every little thing. It's funny to look at my very first weather.com parsing code block; it was a 400 line code block with over 100 variables. Now I've shortened it down to less than 90 lines of code and 10 local/stack variables, and mainly all because I'm using multi-dimensional arrays.
Now I don't know much about structures since I've never used them since the Programmer practical, but I'd like to hear about them and if you use them, how do you use them? Do you find them more flexible / efficient than arrays that COULD be taking up unused memory?
Or are we comparing oranges to applies here?
Before I was introduced to the real power of arrays, I used individual variables for every little thing. It's funny to look at my very first weather.com parsing code block; it was a 400 line code block with over 100 variables. Now I've shortened it down to less than 90 lines of code and 10 local/stack variables, and mainly all because I'm using multi-dimensional arrays.
Now I don't know much about structures since I've never used them since the Programmer practical, but I'd like to hear about them and if you use them, how do you use them? Do you find them more flexible / efficient than arrays that COULD be taking up unused memory?
Or are we comparing oranges to applies here?
0
Comments
Structures are very helpful to manage/store datasets, especially if the are of different type
Or think of phonebook listings where you need full datasets
But depending on the application and functions I need I'm still using arrays.
One disadvantage of structures is that you can't pass them directly in/out of a module....
Structures are by far easier to keep track of. If I have a data listing ... lets say I am controlling thermostats, and am tracking room temperatures, I can make an array for those like so: INTEGER iTemps[NUMBER_ROOMS][3], which holds current temp, heat setpoint, and cool setpoint. But when I refer to iTemps[3][2], the only way I can keep track of what the 2 is in the second index is to make it a CONSTANT reference (which is easy enough to do, but this is just an example, so I'm keeping it simple). In structure format, it would be something like: ... and I could then make an array of these for the rooms -
strTemp nTemps[NUMBER_ROOMS] ;
You would then reference each temperature by nTemps[CURRENT_ROOM].Current, etc., and know exactly what you were referencing without having to look anything up.
In a simple situation like above, I wouldn't bother with the structure. I would just make a couple of CONSTANTs to reference my indices and use a multidimensined array. But if your data was more complex - lets say you were tracking not only temperatures, but setback times and the actual room names too, then a structure would make more sense. But you can't pass it as an argument to a module .
- Chip