Page 104 - 6437
P. 104

When the above code is compiled and executed, it produces the following result:

                 Address of var[3] = bfedbcd8

                 Value of var[3] = 200
                 Address of var[2] = bfedbcd4

                 Value of var[2] = 100
                 Address of var[1] = bfedbcd0

                 Value of var[1] = 10


                          Pointer Comparisons
                          Pointers may be compared by using relational operators, such as ==, <, and >. If p1 and
                   p2 point to variables that are related to each other, such as elements of the same array, then p1 and
                   p2 can be meaningfully compared.
                          The following program modifies the previous example - one by incrementing the variable
                   pointer so long as the address to which it points is either less than or equal to the address of the
                   last element of the array, which is &var[MAX - 1]:


                 #include <stdio.h>


                 const int MAX = 3;


                 int main ()

                 {
                    int  var[] = {10, 100, 200};

                    int  i, *ptr;


                    /* let us have address of the first element in pointer */
                    ptr = var;

                    i = 0;
                    while ( ptr <= &var[MAX - 1] )

                    {


                        printf("Address of var[%d] = %x\n", i, ptr );
                        printf("Value of var[%d] = %d\n", i, *ptr );


                        /* point to the previous location */

                        ptr++;


                                                                                                      10
                                                                                                      7
   99   100   101   102   103   104   105   106   107   108   109