Overview -------- review Arrays review Pointers review binary search Homework binary search. ctype p 166 FILE *, fopen, fclose, fgetc(), fputc(), fseek() tricks: gcc -S t.c gcc -ansi -Wall t.c /* comment comment comment */ Creating arrays --------------- int nums[10]; int nums[10] = { 9, 8, 7, ..., 0 } int nums[10] = { 0 }; Accessing int y = nums[1]; Creating pointers ----------------- int *ip; ip = &num[0]; what is the *ip? 9 ip = &num[2]; what is the *ip? 7 what about *(ip + 1)? 6 int *ip1 = &num[10]; What is ip1 - ip? 10 How about Arrays of pointers? ---------------------------- What is the difference? char name[] = "joe"; char *namep = "joe" ---------- name: |j|o|e|\0| ---------- ---------- namep: | 0xXXXX | ----->0xXXXX : |j|o|e|\0| ---------- char *name = "joe"; char *names[] = { "joe", "jack" }; ------- names:| 124 | -+ | 128 |-+| | | || ... || || 124: |joe\0|-++ 128: |jack |-+ 12c: |\0 | Homework ============= Binary search --------------- 0 1 2 3 4 5 6 8 1, 4, 5, 8,10,12,15,20 ^ ^ ^ Find if we have value 8? int bsearch (int n, int nums[], int size) { l = 0; h = size-1; while (l <=h) { mid = (l+h)/2; if (n < nums[mid]) h = mid - 1; else if (n > nums[mid] l = mid + 1; else return mid; return -1; } Extend this for strings... Searching in dictionary - ignore all punctuation Matching in disctionary - Exact match Ctype ---------- #include isalpha isupper islower isdigit isalnum isspace toupper tolower File I/O -------------- #include stdin, stdout, stderr ----------------- | bytes of file | ----------------- ^ +-fp FILE *fp fopen (, ) => Any pointer to a string of characters = "rwa+" Ex: FILE *dp = fopen ("/usr/dict/words", "r"); fclose fgetc (FILE*) fputc (int c , FILE*) fseek (FILE*, offset, [SEEK_SET, SEEK_CUR, SEEK_END]) int c = getc(dp); why use an int when a char is returned? char 0..255 while EOF == -1