Recall: using pointer arithmetic to access array elements

  • After setting p = &a[0]:

       *(p+0)   ≡   a[0]
       *(p+1)   ≡   a[1]
       *(p+2)   ≡   a[2]
       and so on
    

    Schematically:

An alternate way to process array elements using pointer arithmetic

  • Consider the situation after assigning p = &a[0] and *p = a[0]

  • Question:

      • What is the situation after the statement:   p = p + 1; ?

An alternate way to process array elements using pointer arithmetic

  • After the statement p = p + 1;:   p = &a[1] and *p = a[1]

  • Question:

      • What is the situation after the statement:   p = p + 1; ?

An alternate way to process array elements using pointer arithmetic

  • After the statement p = p + 1;:   p = &a[2] and *p = a[2]

  • And so on:

      • We can use p = p + 1; to traverse an array !!

An alternate way to process array elements using pointer arithmetic

  • Consider the traditional way to tarverse an array:

    int main(int argc, char *argv[])
    {   
        int a[10];
    
    
        for (int i = 0; i < 10; i++ )
            a[i] = i;
    
        // Traditional way to traverse an array
    
    
        for ( int i = 0; i < 10; i++ )
        {
            printf("&d\n", a[i]);
    
        }
    }   

An alternate way to process array elements using pointer arithmetic

  • Using pointer arithmetic, we can also traverse an array using a pointer variable p:

    int main(int argc, char *argv[])
    {   
        int a[10];
        int *p;
    
        for (int i = 0; i < 10; i++ )
            a[i] = i;
    
        // Traversing an array using pointer arithmetic
    
        p = a;
        for ( int i = 0; i < 10; i++ )
        {
            printf("&d\n", *p);
            p = p + 1;   // Move to the next element in array
        }
    }   

DEMO: demo/C/set2/pointer-arithm3a.c
Note:   The traversal code can be shorted using a short hand operator !!

Review: the post-increment ++ operator

  • Review: post increment operator:

     p++     1. return value = p (= the "old" value in p)
             2. p = p + 1  (increment p afterwards)
    

  • Example:

    int main(int argc, char *argv[])
    {
       int x = 4;
    
       printf("%d\n", x++);    // Prints 4 (new value of x = 5)
       printf("%d\n", x++);    // Prints 5 (new value of x = 6)
       printf("%d\n", x++);    // Prints 6 (new value of x = 7)
       printf("%d\n", x++);    // Prints 7 (new value of x = 8)
    }    

DEMO: demo/C/set2/post-inc.c

An alternate way to process array elements using pointer arithmetic   - Revisited

  • Using pointer arithmetic, we can also traverse an array using a pointer variable p:

    int main(int argc, char *argv[])
    {   
        int a[10];
        int *p;
    
        for (int i = 0; i < 10; i++ )
            a[i] = i;
    
        // Traversing an array using pointer arithmetic
    
        p = a;
        for ( int i = 0; i < 10; i++ )
        {
            printf("&d\n", *p);  // Use the value in p
            p = p + 1;   // We incremented p after using the value in p
        }
    }   

Note:   The traversal code can be shorted using a short hand operator !!

An alternate way to process array elements using pointer arithmetic   - Revisited

  • Because we increment p = p + 1 after using *p, we use the post-increment p++ operator:

    int main(int argc, char *argv[])
    {   
        int a[10];
        int *p;
    
        for (int i = 0; i < 10; i++ )
            a[i] = i;
    
        // Traversing an array using pointer arithmetic
    
        p = a;
        for ( int i = 0; i < 10; i++ )
        {
            printf("&d\n", *(p++)); // Use p and afterwards increment p
               
        }
    }   

We can simplify the *(p++) expression because the ++ operator has a higher priority than the * operator ()

An alternate way to process array elements using pointer arithmetic   - Revisited

  • The expression *(p++) can be simplified to *p++

    int main(int argc, char *argv[])
    {   
        int a[10];
        int *p;
    
        for (int i = 0; i < 10; i++ )
            a[i] = i;
    
        // Traversing an array using pointer arithmetic
    
        p = a;
        for ( int i = 0; i < 10; i++ )
        {
            printf("&d\n", *p++);   // Use p and afterwards increment p
                       
        }
    }   

DEMO: demo/C/set2/pointer-arithm3b.c