UMBC CS 201, Spring 02
UMBC CMSC 201 Spring '02 CSEE | 201 | 201 S'02 | lectures | news | help

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.

ptr_add

The Program

/********************************************* File: ptr_add.c Author: S. Bogar Date: 1/1/99 Section: 101 SSN: 123-45-6789 EMail: bogar@cs.umbc.edu Pointer addition is not what you think **********************************************/ #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: ??? Date: ??? SSN: ??? Section: none Email: don't know this either Using pointers to point into array *********************************************/ #include <stdio.h> int main() { int a[10], *p, i ; for(i = 0 ; i < 10 ; i++) { a[i] = i ; } for(i = 0 ; i < 10 ; i++) { printf("a[%d] = %d\n", i, a[i]) ; } printf("\n") ; p = a ; for(i = 0 ; i < 10 ; i++) { *p = *p + 1 ; p = p + 1 ; } for(i = 0 ; i < 10 ; i++) { printf("a[%d] = %d\n", i, a[i]) ; } return 0; }

The Sample Run

a[0] = 0 a[1] = 1 a[2] = 2 a[3] = 3 a[4] = 4 a[5] = 5 a[6] = 6 a[7] = 7 a[8] = 8 a[9] = 9 a[0] = 1 a[1] = 2 a[2] = 3 a[3] = 4 a[4] = 5 a[5] = 6 a[6] = 7 a[7] = 8 a[8] = 9 a[9] = 10

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

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: S. Bogar Date: 2/2/99 Section: 101 SSN: 123-45-6789 EMail: bogar@cs.umbc.edu When is an array not any array? When it is really a pointer. **********************************************/ #include <stdio.h> /* function prototype */ void UseArray(int a[]); int main() { int a[10] ; UseArray(a) ; return 0; } /********************************************* ** Function: UseArray ** Input: an array of integers ** Output: TBD ** ** This function demonstrates how a function ** uses array name as a pointer *********************************************/ void UseArray(int a[]) { int b[10], *temp, i ; temp = a ; a = b ; for(i = 0 ; i < 10 ; i++) { *a = 2 * i ; a++ ; } for(i = 0 ; i < 10 ; i++) { printf("b[%d] = %d\n", i, b[i]) ; } printf("\n") ; a = temp ; for(i = 0 ; i < 10 ; i++) { a[i] = 3 * i ; } for(i = 0 ; i < 10 ; i++) { printf("a[%d] = %d\n", i, a[i]) ; } }

The Sample Run

b[0] = 0 b[1] = 2 b[2] = 4 b[3] = 6 b[4] = 8 b[5] = 10 b[6] = 12 b[7] = 14 b[8] = 16 b[9] = 18 a[0] = 0 a[1] = 3 a[2] = 6 a[3] = 9 a[4] = 12 a[5] = 15 a[6] = 18 a[7] = 21 a[8] = 24 a[9] = 27
Last Modified - Thursday, 17-Jan-2002 13:52:24 EST


CSEE | 201 | 201 S'02 | lectures | news | help