1 More on Pointers
2 Reminder
3 Pointers Pointer is a variable that contains the address of a variable Here P is said to point to the variable C C 7 34…… …… P
4 Brief Summary &x – address (pointer) of variable x *y – content in address y (common) usage: int z = *y; (common) usage: printf(“%d”,*y); int */double */char * - define pointer to the corresponding primitive (common) usage: int * p = &x; int x = 8; int *y = &x; *y = 10; printf(“%d”,*(&x));
5 Common errors It is impossible to define pointers to constants or expressions. i = &3; j = &(k+5); It is also impossible to change a variable’s address (because it is not for us to determine!). &a = &b; &a = 150;
6 Exercise (from last week) Write a function that accepts a double parameter and returns its integer and fraction parts Write a program that accepts a number from the user and prints out its integer and fraction parts, using this function
7 dbl_split.c void split(double num, int *int_part, double *frac_part) { *int_part = (int)num; *frac_part = num - *int_part; }
8 dbl_split.c int main(void) { double num, fraction; int integer; printf("Please enter a double number: "); scanf("%lf", &num); split(num, &integer, &fraction); printf("The integer part is %d\n", integer); printf("The fraction part is %g\n", fraction); return 0; }
9 Pointers and Arrays Recall that an array S holds the address of its first element S[0] S is actually a pointer to S[0] int S[10]; int *P; P=S; /* From now P is equivalent to S */ Both P and S are now pointing to S[0]
10 Pointer versus Array Array is a pointer Array declaration and memory allocation A pointer is uninitialized when declared (like a primitive variable) The value of an array variable (the address it is pointing) cannot be changed – unlike pointers!
11 Pointer arithmetic Pointers can be incremented and decremented If p is a pointer to a particular type, p+1 yields the correct address of the next variable of the same type How? p++, p+i, and p += i also make sense
12 Pointer arithmetic - example If p and q point to elements in an array, q-p yields the number of elements between p and q. int a[] = {0,1,2,3}; int * b = a; b += 4; printf("last element: %d, diff: %d\n",*(b-1),b-a);
13 array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
14 array_strcpy – step by step void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest 0 i ‘!’
15 array_strcpy – step by step void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest 0 i ‘!’
16 array_strcpy – step by step void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] =src[i]; i++; } dest[i] = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest 1 i ‘y’‘!’
17 array_strcpy – step by step void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] =src[i]; i++; } dest[i] = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest 1 i ‘y’‘!’
18 array_strcpy – step by step void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] =src[i]; i++; } dest[i] = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest 1 i ‘y’‘e’‘!’
19 array_strcpy – step by step void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] =src[i]; i++; } dest[i] = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest 2 i ‘y’‘e’‘!’
20 array_strcpy – step by step void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] =src[i]; i++; } dest[i] = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest 2 i ‘y’‘e’‘!’
21 array_strcpy – step by step void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] =src[i]; i++; } dest[i] = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest 2 i ‘y’‘e’‘s’‘!’
22 array_strcpy – step by step void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] =src[i]; i++; } dest[i] = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest 3 i ‘y’‘e’‘s’‘!’
23 array_strcpy – step by step void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] =src[i]; i++; } dest[i] = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest 3 i ‘y’‘e’‘s’‘!’
24 array_strcpy – step by step void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] =src[i]; i++; } dest[i] = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest 3 i ‘y’‘e’‘s’‘\0’‘!’
25 Pointer arithmetic strcpy void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; }
26 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘!’
27 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘!’
28 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘!’
29 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘!’
30 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘!’
31 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘!’
32 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘!’
33 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘!’
34 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘!’
35 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘s’‘!’
36 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘s’‘!’
37 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘s’‘!’
38 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘s’‘!’
39 Strcpy – step by step void my_strcpy(char *dest, char *src) { while (*src != '\0') { *dest = *src; dest++; src++; } *dest = '\0'; } ‘y’‘e’‘s’‘\0’‘%’ src dest ‘y’‘e’‘s’‘\0’‘!’
40 An additional use A function that accepts an array and searches for something within it (a char, a number, etc.) What should the function return? The index where the ‘something’ was found A pointer to the place where it was found
41 Functions that return pointers Like any other data type, functions can return pointers For example, a prototype for a function returning a pointer to char will be – char *func(…); But how would we indicate failure Suppose we searched the array and didn’t find anything – what should we return?
42 The NULL pointer The NULL pointer is an ‘empty’ pointer that points to nothing. It is the address number 0. If a pointer p is set to NULL, trying to access *p results in a run-time (not compilation) error. Often used to indicate failure
43 strchr – pointer arithmetic char *my_strchr(char str[], char c) { char *search = str; while (*search != '\0') { if (*search == c) return search; search++; } return NULL; } ‘h’‘e’‘l’ ‘o’ str ‘\0’ c ‘l’ search
44 strchr – pointer arithmetic char *my_strchr(char str[], char c) { char *search = str; while (*search != '\0') { if (*search == c) return search; search++; } return NULL; } ‘h’‘e’‘l’ ‘o’ str ‘\0’ c ‘l’ search
45 strchr – pointer arithmetic char *my_strchr(char str[], char c) { char *search = str; while (*search != '\0') { if (*search == c) return search; search++; } return NULL; } ‘h’‘e’‘l’ ‘o’ str ‘\0’ c ‘l’ search
46 strchr – pointer arithmetic char *my_strchr(char str[], char c) { char *search = str; while (*search != '\0') { if (*search == c) return search; search++; } return NULL; } ‘h’‘e’‘l’ ‘o’ str ‘\0’ c ‘l’ search
47 strchr – pointer arithmetic char *my_strchr(char str[], char c) { char *search = str; while (*search != '\0') { if (*search == c) return search; search++; } return NULL; } ‘h’‘e’‘l’ ‘o’ str ‘\0’ c ‘l’ search
48 strchr – pointer arithmetic char *my_strchr(char str[], char c) { char *search = str; while (*search != '\0') { if (*search == c) return search; search++; } return NULL; } ‘h’‘e’‘l’ ‘o’ str ‘\0’ c ‘l’ search
49 strchr – pointer arithmetic char *my_strchr(char str[], char c) { char *search = str; while (*search != '\0') { if (*search == c) return search; search++; } return NULL; } ‘h’‘e’‘l’ ‘o’ str ‘\0’ c ‘l’ search
50 strchr – pointer arithmetic char *my_strchr(char str[], char c) { char *search = str; while (*search != '\0') { if (*search == c) return search; search++; } return NULL; } ‘h’‘e’‘l’ ‘o’ str ‘\0’ c ‘l’ search
51 strchr – pointer arithmetic char *my_strchr(char str[], char c) { char *search = str; while (*search != '\0') { if (*search == c) return search; search++; } return NULL; } ‘h’‘e’‘l’ ‘o’ str ‘\0’ c ‘l’ search
52 strchr – pointer arithmetic char *my_strchr(char str[], char c) { char *search = str; while (*search != '\0') { if (*search == c) return search; search++; } return NULL; } ‘h’‘e’‘l’ ‘o’ str ‘\0’ c ‘l’ search
53 Example strchr.c
54 Mystery – what does this do? int main(void) { char s[LENGTH+1]; char *p; scanf("%100s", s); p = strchr(s, ','); while(p!=NULL) { *p = ' '; p = strchr(p+1, ','); } printf("The result is - %s\n", s); return 0; }
55 Exercise Implement the string.h function strrchr that returns a pointer to the last occurrence of a character inside a string. Write a program that accepts a string and char from the user, displays what remains of the string after the last occurrence of that char.
56 strrchr.c char* my_strrchr(char str[], char c) { char *search = str + strlen(str) - 1; /* search now points to the beginning of str */ while(search >= str) { if(*search == c) return search; search--; /* advance pointer by one */ } /* if we're here, we didn't find */ return NULL; }
57 strncmp Declared in string.h, with prototype int strncmp(char *s1, char *s2, int n); Returns 0 if the first n letters of s1 are equal to those of s2 Returns non-zero if first n letters are different
58 Exercise Using strncmp, implement the following function – Input – two strings str1, str2 Output – a pointer to the first instance of str2 in str1, or NULL Write a program that accepts two strings from the user and reports whether the first contains the second
59 strstr.c char* my_strstr(char str1[], char str2[]) { char *search = haystack; /*search now points to the beginning of str*/ int len1 = strlen(str1), len2 = strlen(str2); int i; for(i = 0; i <= len1-len2; i++) { /* strncmp(s1, s2, n) compares the first n letters of s1 and s2 */ if(strncmp(search, str2, len2) == 0) return search; search++; } /* if we're here, we didn't find */ return NULL; }
60 home Implement the following function – Input – two strings str1, str2 Output – pointer to the first instance in str1 of any of the characters contained in str2 Write a program that accepts a string from the user and replaces all punctuation signs (,.;:!?) with spaces
61 solution strcspn.c
62 Some functions of string.h Implementations of functions from string.h : strcpy.c – copies one string into the other strchr.c – returns a pointer to the first occurrence of a character within a string strstr.c – returns a pointer to the first occurrence of one string within another strcspn.c – returns a pointer to the first occurrence of a single char from one string in the second