Pointers to anything x int *x; int **y; double *z; y some *int z some int some double 6
printing an array void print_array(int a[], int len) { for (int i=0; i<len; i++) n o i cout << "[" << i << "] = " ers v ay r r << a[i] << endl; a } void print_array(int *a, int len) { n o i s for (int i=0; i<len; i++) er v r e t in cout << "[" << i << "] = " o p << *a++ << endl; } 9
Passing pointers as parameters void swap( int *x, int *y) { int tmp; tmp = *x; *x = *y; *y = tmp; } 10
String Example - Count the chars int count_string( char *s) { int n=0; ted n i o p g n thi e h t while (*s) { e l i wh ull n t o n s ysi b o t n++; increment count s++; } set s to point to the next char return(n); } 14
Another way int count_string( char *s) { char *ptr = s; while (*ptr) { ptr++; pointer arithmetic! } return(ptr - s); } 15
Exercises (for those so inclined) Write strcpy l Write a function that compares 2 strings and returns true if they are the same string, false if they are not. l Write a function that removes all spaces from a string. l 16