More short hand operators

  • C has many short hand operators

  • These ones you have learned when studying Java:

      ++   --    +=    -=     *=    /=       etc       
    

  • You've learned the  ->  operator when discussing member access using reference variables:

      p->x    ≡   (*p).x
    

  • We will now learn another short hand operator for the expression:

      *(p + k)  // Used to access array elements... 

    Can you guess the symbol(s) of this new short hand operator (it's very intuitive) ?

The short hand operator [ ]  

  • The array selection operator  [ ]  is a binary operator defined as follows:

         p[k]*(p + k)    
    
       where:
    
          p is a  reference typed expression/variable             
          k is an integer   typed expression/variable
    

  • Note:

    • You have already used the array selection operator  [ ]  before:

        int a[10];
      
        a[4] = 1;  // [ ] is the array selection operator 
      

      The data type of the symbolic constant a is an reference type
      (a = the base address of an array)

      So:     a[4] is using pointer arithmetic

Using pointer arithmetic to access elements in an array   - Revisited  

  • Previously, we used *(p + k) as an alias to access a[k]:

    int main(int argc, char *argv[])
    {
        int a[10] = { 11, 22, 33, 44, 55, 66, 77, 88, 99, 777 };
        int* p;
    
        p = &a[0];            // p points to variable a[0]
    
        printf("*(p + 0) = %d, a[0] = %d\n", *(p+0), a[0] );
        printf("*(p + 1) = %d, a[1] = %d\n", *(p+1), a[1] );
        printf("*(p + 2) = %d, a[2] = %d\n", *(p+2), a[2] );
        printf("*(p + 3) = %d, a[3] = %d\n", *(p+3), a[3] );
    }  

    Output:

      *(p + 0) = 11, a[0] = 11  // I.e.:  *(p + k) is 
      *(p + 1) = 22, a[1] = 22  // an alias for a[k] !!
      *(p + 2) = 33, a[2] = 33
      *(p + 3) = 44, a[3] = 44    

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

Using pointer arithmetic to access elements in an array   - Revisited  

  • Using [ ], we can use p[k] as an alias to access a[k]:

    int main(int argc, char *argv[])
    {
        int a[10] = { 11, 22, 33, 44, 55, 66, 77, 88, 99, 777 };
        int* p;
    
        p = &a[0];            // p points to variable a[0]
    
        printf("p[0]     = %d, a[0] = %d\n", p[0]  , a[0] );
        printf("p[1]     = %d, a[1] = %d\n", p[1]  , a[1] );
        printf("p[2]     = %d, a[2] = %d\n", p[2]  , a[2] );
        printf("p[3]     = %d, a[3] = %d\n", p[3]  , a[3] );
    }  

    Output:

      p[0]     = 11, a[0] = 11  // I.e.:  p[k] is 
      p[1]     = 22, a[1] = 22  // an alias for a[k] !!
      p[2]     = 33, a[2] = 33
      p[3]     = 44, a[3] = 44    

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

Using pointer arithmetic to access elements in an array   - Revisited  

  • Note:   pointer arithmetic with a reference variable is more flexible than pointer arithmetic with fixed reference constant (such as base address a ):

    int main(int argc, char *argv[])
    {
        int a[10] = { 11, 22, 33, 44, 55, 66, 77, 88, 99, 777 };
        int* p;
    
        p = &a[1];            // p points to variable a[1]
    
        printf("p[0]     = %d, a[1] = %d\n", p[0]  , a[1] );
        printf("p[1]     = %d, a[2] = %d\n", p[1]  , a[2] );
        printf("p[2]     = %d, a[3] = %d\n", p[2]  , a[3] );
        printf("p[3]     = %d, a[4] = %d\n", p[3]  , a[4] );
    }  

    Output:

      p[0]     = 22, a[1] = 22  // I.e.:  p[k] is 
      p[1]     = 33, a[2] = 33  // an alias for a[k+1] !!
      p[2]     = 44, a[3] = 44
      p[3]     = 55, a[4] = 55    

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