UMBC CS 201, Fall 06
 UMBC CMSC 201 Fall '06 CSEE | 201 | 201 F'06 | lectures | news | help Search Notes:

# Pointer arithmetic

Another word of warning: you are allowed to add constants to a pointer, but the result may not be what you think.

The following example shows that adding 1 to a character pointer, an integer pointer and a double pointer, adds 1, 4 and 8 respectively to the addresses stored in the pointers.

## The Program

/********************************************* File: ptr_add.c Author: R. Chang Modified by: S. Bogar Date: 1/1/99 Section: 101 EMail: bogar@cs.umbc.edu Pointer addition is not what you think - adding 1 to a pointer adds different amounts depending on the type being pointed to. **********************************************/ #include <stdio.h> int main() { char c, *cPtr ; int i, *iPtr ; double d, *dPtr ; cPtr = &c ; iPtr = &i ; dPtr = &d ; printf("\nThe addresses of c, i and d are:\n") ; printf("cPtr = %p, iPtr = %p, dPtr = %p\n", cPtr, iPtr, dPtr) ; cPtr = cPtr + 1 ; iPtr = iPtr + 1 ; dPtr = dPtr + 1 ; printf("\nThe new values of cPtr, iPtr and dPtr are:\n") ; printf("cPtr = %p, iPtr = %p, dPtr = %p\n\n", cPtr, iPtr, dPtr) ; printf("Sizeof char = %d\n", sizeof(char) ) ; printf("Sizeof int = %d\n", sizeof(int) ) ; printf("Sizeof double = %d\n", sizeof(double) ) ; return 0; }

## The Sample Run

The addresses of c, i and d are: cPtr = 0x7ffffaa7, iPtr = 0x7ffffa9c, dPtr = 0x7ffffa90 The new values of cPtr, iPtr and dPtr are: cPtr = 0x7ffffaa8, iPtr = 0x7ffffaa0, dPtr = 0x7ffffa98 Sizeof char = 1 Sizeof int = 4 Sizeof double = 8

## Why?

Pointer addition is defined this way because we want to make it easy for pointers to point to successive elements of an array.

Here is an example:

# array1

## The Program

/********************************************* File: array1.c Author: R. Chang Date: ? Modified by: Sue Evans Date: 3/5/04 Section: 01XX & 02XX Email: bogar@cs.umbc.edu Using pointers to point into array *********************************************/ #include <stdio.h> #define SIZE 10 int main() { int array[SIZE], *ptr, i ; /* store some values in the array */ for(i = 0 ; i < SIZE ; i++) { array[i] = i ; } /* printf the values in the array */ for(i = 0 ; i < SIZE ; i++) { printf("array[%d] = %d\n", i, array[i]) ; } printf("\n") ; /* let p point to the beginning of the array */ ptr = array ; for(i = 0 ; i < SIZE ; i++) { /* add 1 to the element that p is pointing to */ *ptr = *ptr + 1 ; /* move the pointer to the next element */ ptr = ptr + 1 ; } /* printf the values in the array */ for(i = 0 ; i < SIZE ; i++) { printf("array[%d] = %d\n", i, array[i]) ; } return 0; }

## The Sample Run

array = 0 array = 1 array = 2 array = 3 array = 4 array = 5 array = 6 array = 7 array = 8 array = 9 array = 1 array = 2 array = 3 array = 4 array = 5 array = 6 array = 7 array = 8 array = 9 array = 10

In fact, if a variable array is a pointer to the beginning of an array, then the first element of the array can be accessed by deferencing array using the expression *array or by the array element notation array.

When a formal parameter is declared to be an array, it is really a pointer to an array as shown in the following

# array2

## The Program

/********************************************* File: array2.c Author: R. Chang Modified by: S. Evans Date: 3/5/04 Section: 01XX & 02XX EMail: bogar@cs.umbc.edu When is an array not any array? When it is really a pointer ? **********************************************/ #include <stdio.h> #define SIZE 10 /* function prototype */ void UseArray(int a[]); int main() { int a[SIZE] ; UseArray(a) ; return 0; } /********************************************* ** Function: UseArray ** Input: an array of integers ** Output: TBD ** ** This function demonstrates how a function ** uses the array name as a pointer - once the array ** name has been passed to a function it is no ** longer subject to the restrictions of an array ** name and is now treated as just a pointer. *********************************************/ void UseArray(int a[]) { int b[SIZE], *temp, i ; /* keep track of where a originally pointed */ temp = a ; /* let a point to the array b */ a = b ; /* fill the array (b) with 2 * index */ /* by using the pointer a */ for(i = 0 ; i < SIZE ; i++) { *a = 2 * i ; a++ ; } /* print out the contents of the array b */ for(i = 0 ; i < SIZE ; i++) { printf("b[%d] = %d\n", i, b[i]) ; } printf("\n") ; /* let a point back to the original array a */ a = temp ; /* fill the array a with 3 * index */ for(i = 0 ; i < SIZE ; i++) { a[i] = 3 * i ; } /* print out the contents of the array a */ for(i = 0 ; i < SIZE ; i++) { printf("a[%d] = %d\n", i, a[i]) ; } }

## The Sample Run

b = 0 b = 2 b = 4 b = 6 b = 8 b = 10 b = 12 b = 14 b = 16 b = 18 a = 0 a = 3 a = 6 a = 9 a = 12 a = 15 a = 18 a = 21 a = 24 a = 27