### The operators ++ and -- used in pointer arithmetic

• Advanced pointer arithmetic: ++ and −− operators

• Review: post increment/decrement operators

 ``` p++ means: 1. p = p + 1 2. returns the OLD value (before the increment) in p p-- means: 1. p = p - 1 2. returns the OLD value (before the increment) in p ```

• Applying the post increment/decrement operators on reference veriables:

 ``` if p is a reference typed variable, then: p++ means: 1. p = p + 1 and will store p + 1*sizeof( (*p) ) into p 2. returns the OLD value (before the increment) in p p-- means: 1. p = p - 1 and will store p - 1*sizeof( (*p) ) into p 2. returns the OLD value (before the increment) in p ```

• Example:

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

Output:

 ``` staring p = 4290769444 // p++ increases p by 4 = sizeof(int) !!! after p++: p = 4290769448 after p++: p = 4290769452 after p++: p = 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-arithm3.c To run:          ./a.out

• Advanced pointer arithmetic expression:   *(p++)   and   *(p--)

• Meaning of the expression *(p++):

 ``` means *(p++) <===> *( p++ ) // apply p++ first p++ : 1. p = p+1 2. returns the OLD value of p !!! <===> *( use the OLD value of p ) <===> the variable pointed to by the OLD value of p !!! ```

In plain English:

 Use the (array) element that is currently pointed to by p Increment p to point to the next (array) element

• Example:

 ``` int main(int argc, char *argv[]) { int a[10] = { 11, 22, 33, 44, 55, 66, 77, 88, 99, 777 }; int i; int *p; printf("Array a[]: "); for ( i = 0; i < 10; i++ ) printf("%d ", a[i] ); putchar('\n'); p = &a[0]; // p points to variable a[0] printf("*(p++) = %d\n", *(p++) ); printf("*(p++) = %d\n", *(p++) ); printf("*(p++) = %d\n", *(p++) ); printf("*(p++) = %d\n", *(p++) ); } ```

Output:

 ``` Array a[]: 11 22 33 44 55 66 77 88 99 777 *(p++) = 11 *(p++) = 22 *(p++) = 33 *(p++) = 44 ```

Explanation:

• Initially, p points to a[0]

• Then the expression:

 ``` *(p++) ```

will return a[0] because p++ uses the old value (= &a[0]) of p.

After this expression is evaluated, the variable p will point to a[1] !

• Then in the second iteration, the expression:

 ``` *(p++) ```

will return a[1] !!

After this expression is evaluated, the variable p will point to a[2] !

• And in the 3rd iteration, the expression:

 ``` *(p++) ```

will return a[2] !!

After this expression is evaluated, the variable p will point to a[2] !

• And so on....

• 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-arithm4.c To run:          ./a.out

• Advanced example: copy and array with pointer arithmetic

• Here is a traditional program to copy an array:

 ``` int main(int argc, char *argv[]) { int a[10] = { 11, 22, 33, 44, 55, 66, 77, 88, 99, 777 }; int b[10]; int i; for ( i = 0; i < 10; i++ ) b[i] = a[i]; printf("Array a[]: "); for ( i = 0; i < 10; i++ ) printf("%d ", a[i] ); putchar('\n'); printf("Array b[]: "); for ( i = 0; i < 10; i++ ) printf("%d ", b[i] ); putchar('\n'); } ```

• Example Program: (Demo above code)

How to run the program:

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

• Here is how you can use pointer arithmetic to perform the array copy operation:

 ``` int main(int argc, char *argv[]) { int a[10] = { 11, 22, 33, 44, 55, 66, 77, 88, 99, 777 }; int b[10]; int i; int *p, *q; // Defines 2 reference variables // Incorrect: int *p, q; - q will be an int p = &a[0]; q = &b[0]; for ( i = 0; i < 10; i++ ) *(q++) = *(p++); printf("Array a[]: "); for ( i = 0; i < 10; i++ ) printf("%d ", a[i] ); putchar('\n'); printf("Array b[]: "); for ( i = 0; i < 10; i++ ) printf("%d ", b[i] ); putchar('\n'); } ```

• Example Program: (Demo above code)

How to run the program:

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

• Common practice of C programmers....

• C fact:

• C programs often use pointer arithmetic to process elements in an array

Example:

 ``` p = &a[0]; // or p = a; for ( i = 0; i < 10; i++ ) *(p++) = .....; instead of: for ( i = 0; i < 10; i++ ) a[i] = .....; ```

• Reason:

• Efficiency:

• The number of memory accesses needed to obtain the value using a[i] is:

 ``` 1 memory read to get the value of i 1 memory read to get the value of a[i] ------------------------------------------- 2 memory read operations total ```

• When the pointer variable p is stored in a register (inside the CPU), the number of memory accesses needed to obtain the value using *(p++) is:

 ``` 1 memory read to get the value of *p 0 memory read to update p++ (in register) -------------------------------------------------------- 1 memory read operations total ```

• A shorter notation: *p++ instead of *(p++)

• Fact:

 The post increment operator ++ has a higher precedence than the de-reference operator *         See: click here

• Therefore: *p++ is evaluated as follows:

 ``` *p++ ===> *p++ // * and ++ has same priority // ==> Use associativity rule // ==> Perform operations from right-to-left ! // ==> Perform ++ first !!! ===> *(p++) ```

• Conclusion:

• We could have written the array copy program like like:

 ``` int main(int argc, char *argv[]) { int a[10] = { 11, 22, 33, 44, 55, 66, 77, 88, 99, 777 }; int b[10]; int i; int *p, *q; // Defines 2 reference variables // Incorrect: int *p, q; - q will be an int p = &a[0]; q = &b[0]; for ( i = 0; i < 10; i++ ) *q++ = *p++; printf("Array a[]: "); for ( i = 0; i < 10; i++ ) printf("%d ", a[i] ); putchar('\n'); printf("Array b[]: "); for ( i = 0; i < 10; i++ ) printf("%d ", b[i] ); putchar('\n'); } ```

• Very frequently used expression in C:

 ``` DataType * p; *p++ ```

*p++ means:

 Use the variable (array element) that is currently pointed to by p           Incremenet p to point to the next (array) element