| 159 | === Structures === |
| 160 | |
| 161 | A structure can contains any variable types sections, and also arrays and sub-structures |
| 162 | |
| 163 | {{{ |
| 164 | #!C |
| 165 | /* Note: 'time' is a previously-defined structure' */ |
| 166 | struct time lap[MAX_LAPS]; |
| 167 | /* This call creates an array called 'lap' of MAX_LAPS elements that are structures of type 'time' */ |
| 168 | |
| 169 | /* You can also set values like */ |
| 170 | lap[count].hour = hour; |
| 171 | /* This set the value 'hour' on the 'hour' (structure element) of the 'count' (array element) of lap */ |
| 172 | |
| 173 | /* Initialization of an array of structures: */ |
| 174 | struct time start_stop[2] = { |
| 175 | { 10, 0 }, |
| 176 | { 12, 0 } |
| 177 | }; |
| 178 | }}} |
| 179 | |
| 180 | |
| 181 | ==== Packaged structures ==== |
| 182 | Image an application that stupidly takes a lot of storage but very few 'cpu' usage and you will need to optimize it for data, you can set the value of the bits to use for the structure elements, like: |
| 183 | {{{ |
| 184 | #!C |
| 185 | struct item { |
| 186 | unsigned int seen:1; |
| 187 | ... |
| 188 | } |
| 189 | /* Now the variable 'seen' (seen or not seen) only takes one BIT */ |
| 190 | }}} |
| 191 | The bad point of this usage is that it the access to this data is pretty more slow than in normal mode, so do this only when is really needed to save storage space. |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | === Pointers === |
| 199 | |
| 200 | With pointers you can modify directly the data of a variable (see #functions) |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | === Typedef === |
| 209 | |
| 210 | With typedef we can define our own variable types, for example: |
| 211 | {{{ |
| 212 | #!C |
| 213 | typedef static long int mylongintstatic; |
| 214 | |
| 215 | /* example of declare a variable of this new type: */ |
| 216 | mylongintstatic var1 = 5; |
| 217 | }}} |
| 218 | |
| 219 | Note that ''typedef'' could be considered similar to the #define statement, but can define more complex objects like: |
| 220 | {{{ |
| 221 | #!C |
| 222 | /* group is a type of 10-elements array */ |
| 223 | typedef int group[10] |
| 224 | }}} |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | === Conversion === |
| 232 | |
| 233 | You can convert a variable type to other at any time by: |
| 234 | {{{ |
| 235 | #!C |
| 236 | int value = 5; |
| 237 | printf ("%f", (float)value ); |
| 238 | }}} |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | === Misc === |
| 247 | |
| 248 | The '''header''' files should be limited to have only types and definitions |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |