boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

103-ptr-arith-sub.c (533B)


      1 /* Pointer subtraction yielding ptrdiff_t element count, plus
      2  * mixing with array indexing. */
      3 
      4 int main(int argc, char **argv) {
      5     int a[10];
      6     int *p = a;
      7     int *q = a + 7;
      8     if (q - p != 7) return 1;
      9 
     10     /* Pointer minus integer. */
     11     int *r = q - 3;
     12     if (r - p != 4) return 2;
     13 
     14     /* Index a pointer the same as the array. */
     15     int i;
     16     for (i = 0; i < 10; i = i + 1) a[i] = i * i;
     17     if (p[5] != 25)        return 3;
     18     if (*(p + 5) != 25)    return 4;
     19     if ((q - 2)[0] != 25)  return 5;
     20     return 0;
     21 }