Sunday, 13 October 2013

use of array versus pointer references

     #include <stdio.h>

     # define MAXARRAYSIZE 5
     main()
     {
        int intarray[MAXARRAYSIZE];
        int *iap;   /* a pointer to an int */
        int k;      /* loop induction variable */

        /* one implementation */
        iap = intarray;
        for ( k = 0; k < MAXARRAYSIZE; k++) {
            *iap = k + 1;
            iap++;
        }
        iap = intarray;
        for ( k = 0; k < MAXARRAYSIZE; k++) {
            printf("%d\n", *iap);
            iap++;
        }

        /* another implementation */
        for ( k = 0; k < MAXARRAYSIZE; k++) {
            intarray[k] = k + 1;
        }
        for ( k = 0; k < MAXARRAYSIZE; k++) {
            printf("%d\n", intarray[k]);
        }
     }

No comments:

Post a Comment