Page 96 - 6437
P. 96

*(p + 8) : 1901409888

                            *(p + 9) : 1990469526


                          Pointer to an Array
                          It is most likely that you would not understand this section until you are through with the
                   chapter ‘Pointers’.
                          Assuming you have some understanding of pointers in C, let us start: An array name is a
                   constant pointer to the first element of the array. Therefore, in the declaration:
                          balance is a pointer to &balance[0], which is the address of the first element of the array


                 double balance[50];

                   balance. Thus, the following program fragment assigns p as the address of the first element of
                   balance:

                            double *p;

                            double balance[10];


                            p = balance;

                          It is legal to use array names as constant pointers, and vice versa. Therefore,
                          *(balance + 4) is a legitimate way of accessing the data at balance[4].
                          Once you store the address of the first element in ‘p’, you can access the array elements
                   using  *p,  *(p+1),  *(p+2),  and  so  on.  Given  below  is  the  example  to  show  all  the  concepts
                   discussed above:


                 #include <stdio.h>


                 int main ()

                 {
                    /* an array with 5 elements */

                    double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
                    double *p;

                    int i;


                    p = balance;


                    /* output each array element's value */








                                                                                                      99
   91   92   93   94   95   96   97   98   99   100   101