Function Pointer and C Standard Library
Total Page:16
File Type:pdf, Size:1020Kb
Function Pointer and C Standard Library 魏恒峰 [email protected] 2017 年 11 月 24 日 . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 1 / 22 Function Pointer . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 2 / 22 sort_ints sort_floats sort_strings sort_persons ··· Compare & Swap Sort . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 3 / 22 sort_ints sort_floats sort_strings sort_persons ··· Sort Compare & Swap . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 3 / 22 sort_ints sort_floats sort_strings sort_persons ··· Sort for any types Compare & Swap . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 3 / 22 Sort for any types Compare & Swap sort_ints sort_floats sort_strings sort_persons ··· . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 3 / 22 Sort for any types Compare & Swap sort_ints sort_floats sort_strings sort_persons ··· . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 3 / 22 (compare.c) (#include <stdlib.h>) void qsort (void *base, size_t num, size_t size, int (*compar)(const void*, const void*)); . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 4 / 22 (#include <stdlib.h>) void qsort (void *base, size_t num, size_t size, int (*compar)(const void*, const void*)); (compare.c) . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 4 / 22 int n = 5; fptr = square; // fptr points to a function fptr(n); // call ''square'' (fptr-square.c) int (*fptr)(int); // fptr is a function pointer int square(int num) { return num * num; } . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 5 / 22 (fptr-square.c) int (*fptr)(int); // fptr is a function pointer int square(int num) { return num * num; } int n = 5; fptr = square; // fptr points to a function fptr(n); // call ''square'' . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 5 / 22 int (*fptr)(int); // fptr is a function pointer int square(int num) { return num * num; } int n = 5; fptr = square; // fptr points to a function fptr(n); // call ''square'' (fptr-square.c) . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 5 / 22 void filter(int *vals , int n, predicate cond); (filter-fp.c) typedef int (*predicate)(int n); // type! . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 6 / 22 (filter-fp.c) typedef int (*predicate)(int n); // type! void filter(int *vals , int n, predicate cond); . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 6 / 22 typedef int (*predicate)(int n); // type! void filter(int *vals , int n, predicate cond); (filter-fp.c) . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 6 / 22 C Standard Library . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 7 / 22 . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 8 / 22 . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 9 / 22 int *vals = malloc(sizeof(int) * n); assert(vals != NULL) (#include <assert.h>) scanf(''%d'', &n); assert(n > 0) . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 10 / 22 (#include <assert.h>) scanf(''%d'', &n); assert(n > 0) int *vals = malloc(sizeof(int) * n); assert(vals != NULL) . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 10 / 22 (#include <ctype.h>) isdigit isalpha isalnum islower isupper isspace tolower toupper . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 11 / 22 (#include <limits.h>) CHAR_MIN CHAR_MAX INT_MIN INT_MAX . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 12 / 22 (#include <math.h>) sin cos exp log pow sqrt ceil floor . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 13 / 22 va_list // type va_start (3) “··· ” must be at the end va_arg (4) ≥ 1 named argument va_end (miniprintf.c) (#include <stdarg.h>) int printf(const char *format, ...); printf(''%d %c %s'', num, ch, str); (1) unnamed (2) # unknown . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 14 / 22 va_list // type va_start va_arg va_end (miniprintf.c) (#include <stdarg.h>) int printf(const char *format, ...); printf(''%d %c %s'', num, ch, str); (1) unnamed (2) # unknown (3) “··· ” must be at the end (4) ≥ 1 named argument . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 14 / 22 (miniprintf.c) (#include <stdarg.h>) int printf(const char *format, ...); printf(''%d %c %s'', num, ch, str); (1) unnamed va_list // type (2) # unknown va_start (3) “··· ” must be at the end va_arg (4) ≥ 1 named argument va_end . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 14 / 22 (#include <stdarg.h>) int printf(const char *format, ...); printf(''%d %c %s'', num, ch, str); (1) unnamed va_list // type (2) # unknown va_start (3) “··· ” must be at the end va_arg (4) ≥ 1 named argument va_end (miniprintf.c) . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 14 / 22 (add-cmdarg.c) (man ls) (Command-line arguments) int main(int argc , char *argv[]) { } argv[0]: program name . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 15 / 22 (man ls) (Command-line arguments) int main(int argc , char *argv[]) { } argv[0]: program name (add-cmdarg.c) . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 15 / 22 (Command-line arguments) int main(int argc , char *argv[]) { } argv[0]: program name (add-cmdarg.c) (man ls) . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 15 / 22 sizeof(int) void* malloc (size_t size); (#include <stddef.h>) typedef /*implementation -defined*/ size_t; . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 16 / 22 (#include <stddef.h>) typedef /*implementation -defined*/ size_t; sizeof(int) void* malloc (size_t size); . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 16 / 22 (#include <stdio.h>) scanf printf getchar putchar fopen fclose EOF . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 17 / 22 (#include <stdlib.h>) atoi atof srand rand malloc free bsearch qsort . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 18 / 22 strncopy strncat strncmp ‘\0’ strlen strchr strrchr strstr strtok (#include <string.h>) char book[] = ''The C Book''; char *pbook = ''The C Book''; . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 19 / 22 ‘\0’ (#include <string.h>) char book[] = ''The C Book''; strncopy char *pbook = ''The C Book''; strncat strncmp strlen strchr strrchr strstr strtok . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 19 / 22 (#include <string.h>) char book[] = ''The C Book''; strncopy char *pbook = ''The C Book''; strncat strncmp ‘\0’ strlen strchr strrchr strstr strtok . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 19 / 22 (#include <string.h>) char book[] = ''The C Book''; strncopy char *pbook = ''The C Book''; strncat strncmp ‘\0’ strlen strchr strrchr strstr strtok . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 19 / 22 (#include <time.h>) . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 20 / 22 Highly Recommended! . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 21 / 22 Highly Recommended! . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 21 / 22 . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 22 / 22 . Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 22 / 22.