173x Filetype PDF File size 0.83 MB Source: fac-staff.seattleu.edu
Pointers What is a pointer o A variable whose value is the address of another variable Pointers and Arrays o p is a pointer to variable v p: 11 23 Operations o &: address of (reference) v: 1000 11 o *: indirection (dereference) CS 217 Declaration mimics use 0 o int *p; pis the address of an int (dereference p is an integer) o int v; p = &v; pstores the address of v 1 2 Pointer Operation Examples More Pointer Examples Examples of * and & References (e.g., *p) are variables int x, y, *p; int x, y, *px, *py; p = &x; /* p gets the address of x */ y = *p; /* y gets the value point to by p */ px = &x; /* px is the address of x*/ y = *(&x); /* same as y = x */ *px = 0; /* sets x to 0 */ Unary operators associate right to left py = px; /* py also points to x*/ y = *&x; /* same as y = *(&x) */ *py += 1; /* increments x to 1 */ y = (*px)++; /* sets y to 1, x to 2 */ Unary operators bind more tightly than binary ones What about the following? y = *p + 1; /* same as y = (*p) + 1; */ ++*px y = *p++; /* same as y = *(p++); */ *px++ 3 4 Argument Passing Pointers and Arrays C functions pass arguments “by value” Pointers can “walk along” arrays To pass arguments “by reference,” use pointers int a[10], *p, x; void swap(int x, int y) void swap(int *x, int *y) p = &a[0]; /* p gets the address of a[0] */ { { x = *p; /* x gets a[0] */ int t; int t; x = *(p+1); /* x gets a[1] */ t = x; t = *x; x = y; *x = *y; p = p + 1; /* p points to a[1] */ y = t; *y = t; p++; /* p points to a[2] */ } } int a = 3, b = 7; int a = 3, b = 7; What about the following? swap(a, b); swap(&a, &b); printf(“%d %d\n”,a,b); printf(“%d %d\n”,a,b); x 3 x 7 x x x = *p++; y 7 y 3 y y x = ++*p; a 3 a 3 a 3 a 7 b 7 b 7 b 7 b 3 5 6 Pointers and Arrays, cont’d Pointer Arithmetic Array names are constant pointers Pointer arithmetic takes into account the stride (size of) the int a[10], *p, i; value pointed to p = a; /* p points to a[0] */ long *p; a++; /* Illegal; can’t change a constant */ p += i; /* increments p by i elements */ p++; /* Legal; p is a variable */ p -= i; /* decrements p by i elements */ Subscripting is defined in terms of pointers p++; /* increments p by 1 element */ a[i], *(a+i), i[a] /* Legal and the same */ p--; /* decrements p by 1 element */ &a[i], a+i /* Legal and the same */ If p and q are pointers to same type T p = &a[0] /* &*(a+0) Æ &*a Æ a */ p – q /* number of elements between p and q */ Pointers can walk arrays efficiently Does it make sense to add two pointers? p = a; for (i = 0; i < 10; i++) printf( “%d\n”, *p++ ); 7 8 Pointer Arithmetic, cont’d Pointer & Array Parameters Comparison operations for pointers Formals are not constant; they are variables o <, >, <=, >=, ==, != o if (p < q) ... ; Passing an array passes a pointer to 1st element o pand qmust point to the same array Arrays (and only arrays) are passed “by reference” o no runtime checks to ensure this An example void f(T a[]) {. . .} int strlen(char *s) { is equivalent to char *p; for (p = s; *p; p++) void f(T *a) {. . .} ; return p – s; } 9 10 Pointers & Strings An Example: String Copy A C string is an array of “char” with NULL at the end Array version void scopy(char s[], char t[]) { String constants denote constant pointers to actual chars int i = 0; char *msg = “now is the time”; while ((s[i] = t[i]) != ‘\0’) i++; char amsg[] = “now is the time”; } char *msg = amsg; Pointer version /* msgpoints to 1st character of “now is the time” */ void scopy(char *s, char *t) { while (*s = *t) { Strings can be used whenever arrays of chars are used s++; t++; putchar(“0123456789”[i]); } } static char digits[] = “0123456789”; Idiomatic version putchar(digits[i]); void scopy(char s[], char t[]) { while (*s++ = *t++) ; 11 } 12 Arrays of Pointers Arrays of Pointers, cont’d Used to build tabular structures Initialization example Indirection “*” has lower precedence than “[]” char *month(int n) { static char *name[] = { Declare array of pointers to strings “January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December” char *line[100]; }; char *(line[100]); Reference examples assert(n >= 1 && n <= 12); return name[n-1]; } line[i] /* refers to the i-th string */ Another example *line[i] /* refers to the 0-th char of the i-th string */ int a, b; int *x[] = {&a, &b, &b, &a, NULL}; 13 14 Arrays of Pointers, cont’d More Examples An array of pointers is a 2-D array Equivalence example int a[10][10]; void f(int *a[10]); int *b[10]; void f(int **a); Array a: Another equivalance example o 2-dimensional 10x10 array void g(int a[][10]); o Storage for 100 elements allocated at compile time void g(int (*a)[10]); o Each row of a has 10 elements, cannot change at runtime Legal in both f and g: o a[6] is a constant **a = 1; Array b: o An array of 10 pointers; each element could point to an array o Storage for 10 pointers allocated at compile time o Values of these pointers must be initialized at runtime o Each row of b can have a different length (ragged array) o b[6] is a variable; b[i] can change at runtime 15 16
no reviews yet
Please Login to review.