Review:   storage structure of arrays

  • Elements of an array are stored consecutively in memory

    Example:

     

The base address of arrays

  • The base address of the array a[ ] is the location of the first element.

    Example:

  • In C, the base address of array a is equal to &a[0] or a

The address of the int typed array element a[k]

  • Because each array element in an int typed array is 4 bytes....

    the address of the array element a[k] ( &a[k]) can be computed as follows:

    For an int typed array &a[k] = baseAddress + 4×k = baseAddress + sizeof(int)×k

The address of the double typed array element a[k]

  • Because each array element in an double typed array is 8 bytes....

    the address of the array element a[k] ( &a[k]) can be computed as follows:

    For an double typed array &a[k] = baseAddress + 8×k = baseAddress + sizeof(double)×k

The address of the array element a[k] - for any data type

  • Because each array element in an dataType typed array is sizeof(dataType)....

    the address of the array element a[k] ( &a[k]) can be computed as follows:

    For any dataType typed array &a[k] = baseAddress + sizeof(dataType)×k

C uses this relationship to define the  +  operator on reference data types

The + operator on reference typed values

  • Definition:

     Suppose the reference variable p is defined as:
    
           dataType *p;
    
     Then the result of the addition of p with an integer n
    
            p + n 
    
     is equal to address value:
    
            p + sizeof(dataType)*n
    
    
    The data type of the result is: dataType *

  • This definition of the  +  operation is called: pointer arithmetic
    (It defines the  +  arithmetic operation on pointer (reference) typed values)

The + operator on reference typed values   - Illustrated

  • After the assignment p = &a[0], the reference variable p contains the base address of the array a:

The + operator on reference typed values   - Illustrated

  • The expression p + 1 is evaluated (by the C compiler) as p + sizeof(int)*1 = 5004:

The + operator on reference typed values   - Illustrated

  • The expression p + 2 is evaluated (by the C compiler) as p + sizeof(int)*2 = 5008:

    And so on

The + operator on reference typed values  - Example

  • Example C program:

       int main(int argc, char *argv[])
       {
           int a[10];
           int *p;
        
           p = &a[0];        // p points to variable a[0]
        
           printf("p     = %lu, &a[%d] = %lu\n", p,   0, &a[0] );
           printf("p + 1 = %lu, &a[%d] = %lu\n", p+1, 1, &a[1] );
           printf("p + 2 = %lu, &a[%d] = %lu\n", p+2, 2, &a[2] );
           printf("p + 3 = %lu, &a[%d] = %lu\n", p+3, 3, &a[3] );
       } 

    Output:

     p     = 4290769444, &a[0] = 4290769444  (increased by 4 !!!)
     p + 1 = 4290769448, &a[1] = 4290769448  (sizeof(int) = 4)
     p + 2 = 4290769452, &a[2] = 4290769452
     p + 3 = 4290769456, &a[3] = 4290769456  

DEMO: demo/C/set2/pointer-arithm1.c --- try: double, short...

Postscript:   the - operator on reference typed variables
  • The operator on reference typed values is defined similarly:

     Suppose the reference variable p is defined as:
    
           dataType *p;
    
     Then the result of the addition of p with an integer n
    
            pn 
    
     is equal to address value:
    
            p − sizeof(dataType)*n
    
    
    The data type of the result is: dataType *

  • The operator on reference typed values is rarely used with integer values

A common application of pointer arithmetic

  • A common usage of pointer arithmetic: find the address &a[k]:

     Suppose we have an array:
    
            int a[10];
    
     When we initialize a reference variable p with a (or &a[0]):
    
            int  *p = a (or &a[0]);  // a[0]a
    
     Then the expression:
    
            p + k = &a[k] (= address of the array element a[k])  
    )

  • In the next set of slides, I will show you an alternate way to access array elements using pointer arithmetic