Page t043, go to NEXT, PREVIOUS, INDEX

Examples of Pointer Arithmetic


Reverse elements of an array

      float x[10];
      /* initialize x */
      { float *left  = &x[0];
        float *right = &x[9];
        while ( left < right )
        { float temp = *left;
          *left++  = *right;
          *right-- = temp;
        }
      }

Set elements of an array to zero

      float x[10];
      float *p = &x[10];  /* uh?? */
      while ( p != x)  *--p = 0.0;

C Course, 22-jan-1997, Peter Klok, pfk@hef.kun.nl