Pointer arithmetic

• Review of reference (pointer) variables

• Recall that:

 ``` int i; // An integer variable int a[10]; // An integer array int *p; // A reference variable to an int variable // p can contain an address of an int variable ```

• Review: access variables through a reference variable

• Consider these 2 kinds of int variables:

 ``` int i; // An integer variable int a[10]; // An integer array ```

Illustrated:

Notice that:

 i is an int typed variable a[0] is an int typed variable a[1] is an int typed variable And so on...

• Therefore, we can use an int *p variable to access any one of these int typed variables:

 ``` int *p; p = &i; *p will access i p = &a[0]; *p will access a[0] p = &a[1]; *p will access a[1] ```

• Pointer arithmentic: the + operator on reference variables

• Definition: pointer addition

• The expression:

 ``` referenceVariable + integer ```

evaluates (returns) to the address

 ``` referenceVariable + integer*sizeof( (*referenceVariable) ) ```

Example:

• Initially, the variable p points to the variable a[0]:

 ``` int a[10]; int *p; p = &a[0]; ```

• Then the expression p + 1 will evaluates to:

 ``` p + 1 ===> &a[0] + 1*sizeof( (*p) ) // (*p) is an int ! <===> &a[0] + 1*4 <===> &a[0] + 4 ```

• The expression p + 2 will evaluates to:

 ``` p + 2 ===> &a[0] + 1*sizeof( (*p) ) // (*p) is an int ! <===> &a[0] + 2*4 <===> &a[0] + 8 ```

Example C progra:

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

Output of C program:

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

• Example Program: (Demo above code)

How to run the program:

 Right click on link(s) and save in a scratch directory To compile:   gcc pointer-arithm1.c To run:          ./a.out

• The following figure shows the result of the p+1 and p+2 more clearly:

In other words:

 ``` If p points to a[0] then: p + 1 = &a[0] + 4 = the address of the array element a[1] !! p + 2 = &a[0] + 8 = the address of the array element a[2] !! ```

• In general:

• The expression:

 ``` p + x ```

evaluates to:

 the address of the xth array element following the array element that is pointed to by p !!

• The operator on reference variables

• The operator on reference variables has a similar effect as the + operator:

• Suppose, the variable p points to the variable a[5]:

 ``` int a[10]; int *p; p = &a[5]; ```

• Then the expression p - 1 will evaluates to:

 ``` p - 1 ===> &a[5] - 1*sizeof( (*p) ) // (*p) is an int ! <===> &a[5] - 1*4 <===> &a[5] - 4 ```

• The expression p - 2 will evaluates to:

 ``` p - 2 ===> &a[5] - 1*sizeof( (*p) ) // (*p) is an int ! <===> &a[5] - 2*4 <===> &a[5] - 8 ```

In other words:

 ``` If p points to a[5] then: p - 1 = &a[5] - 4 = the address of the array element a[4] !! p - 2 = &a[5] - 8 = the address of the array element a[3] !! ```

• In general:

• The expression:

 ``` p - x ```

evaluates to:

 the address of the xth array element prior to the array element that is pointed to by p !!