Changes between Version 5 and Version 6 of ResumedC


Ignore:
Timestamp:
Apr 28, 2009, 5:18:34 PM (16 years ago)
Author:
Thanatermesis
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ResumedC

    v5 v6  
    9393
    9494
    95 
    9695=== Variables ===
    9796
     
    106105'''signed''': means ''with sign'', so, values negative and positive, for example if the '''char''' variable has a limit of 255 values, using the '''sign''' mode we can use from '''-128''' (negative) to '''127''' (positive), and using '''unsigned''' we can use from '''0''' to '''255'''
    107106
     107
     108
     109
     110=== Constants ===
     111
    108112The '''constant''' variables are constants, so they can't change, by convention they are in upper-case
     113
     114{{{
     115#!C
     116/* The variable answer_p is a constant and can't change, but the pointer address can be changed. */
     117const char *answer_p = "Forty-Two";
     118
     119/* If we want to have the inverse to this, we need to put the const AFTER the *, so: */
     120char *const answer_p = "Forty-Two";
     121   /* Now the pointer can't change the address value, but the data (Forty-Two) can be changed */
     122
     123/* Finally, we can set both in constant with something like: */
     124const char *const answer_p = "Forty-Two";
     125}}}
    109126
    110127
     
    157174
    158175
     176
     177
     178
    159179=== Structures ===
    160180
     
    172192
    173193/* Initialization of an array of structures: */
    174 struct time start_stop[2] = {
    175   { 10, 0 },
    176   { 12, 0 }
    177 };
    178 }}}
    179 
     194struct time start_stop[2] =
     195   {
     196      { 10, 0 },
     197      { 12, 0 }
     198   };
     199}}}
    180200
    181201==== Packaged structures ====
     
    196216
    197217
     218
     219
    198220=== Pointers ===
    199221
    200 With pointers you can modify directly the data of a variable (see #functions)
     222With pointers you can modify directly the data of a variable (see the '''functions''' section)
     223
     224You can access to a lot more fast way to structure elements and other big pieces that are inside big ones, so you can optimize your code with the correct usage of pointers,
     225
     226If you want to have a pointer to an array, you need to point to the first array of the variable, so:
     227{{{
     228#!C
     229/* array of 5 elements */
     230char arr[5];
     231
     232/* pointer 'arr_p' to the array 'arr'
     233char *arr_p = &arr[0];
     234
     235*(array_p + 1) /* Equivalent */
     236(*array_p + 1 ) /* Not Equivalent */
     237}}}
     238
    201239
    202240
     
    254292
    255293
     294=== Optimizations ===
     295
     296An array do increments of the array level in order to search/find something, by using pointers is a lot faster than generate an index operation, for example:
     297{{{
     298#!C
     299while ((*array_p) != 0)
     300   ++array__p;
     301}}}
     302
     303Imagine a structure of 226 bytes long, for example, that's a lot of data to move around, so you can declare an array of pointers:
     304{{{
     305#!C
     306struct mailing {
     307   ...
     308} list[MAX_ENTRIES];
     309
     310struct mailing *list_p[MAX_ENTRIES];
     311
     312for (current...
     313   list_p[current] = &list[current];
     314}}}
     315 Now you have to move only 4-byte around, instead of 226 all the time
     316
     317
     318
     319
     320
     321
     322
    256323
    257324
     
    261328
    262329If you want to pass something defined (to use with #ifdef, for example) in the moment of the compilation, you will add options to the compiler (gcc) like -DDEBUG or -DMAX=10
     330
     331
     332
     333
    263334
    264335