Page 82 - Programming Microcontrollers in C
P. 82

Pointers     67

                   int able[10];
                   int *p;
                              After it has been properly declared, when the name able is invoked
                          without the square brackets, the name is a pointer to the first location in
                          the array. So the following two statements are equivalent:

                   p = &able[0];
                              and

                   p = able;
                              Of course, the nth element of the array may be addressed by

                   p = &able[n];
                              The unary pointer operators have higher precedence than the arith­
                          metic operators. Therefore,

                   *pi = *pi + 10;
                   y = *pi + 1;
                   *pi += 1;

                          all result in the integers being altered rather than the pointers. In the
                          first case, the integer pointed to by pi will be increased by 10. In the
                          second case, y will be replaced by one more than the integer pointed
                          to by pi. Finally, the integer pointed to by pi will be increased by
                          1. The statement
                   ++*pi;

                          causes the integer pointed to by pi to be increased by 1. Both ++
                          and the unary * associate from right to left, so in the following case
                   *pi++;

                          the pointer pi is incremented after the dereference operator is applied.
                          Therefore, the pointer is incremented in this case, and the integer *pi
                          remains unaltered. If you wish to post-increment the integer *pi, use
                   (*pi)++;
                              At times, it is necessary to pre-increment the pointer before the
                          dereference. In these cases, use

                   *++pi;
   77   78   79   80   81   82   83   84   85   86   87