<<

Function Pointer and Standard

魏恒峰

[email protected]

2017 年 11 月 24 日

...... Hengfeng Wei ([email protected]) Function Pointer and C 2017 年 11 月 24 日 1 / 22 Function Pointer

...... Hengfeng Wei ([email protected]) Function Pointer and 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 )

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 )

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)

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 )

scanf(''%'', &n); assert(n > 0)

...... Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 10 / 22 (#include )

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 )

isdigit isalpha isalnum

islower isupper

isspace

tolower toupper

...... Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 11 / 22 (#include )

CHAR_MIN CHAR_MAX

INT_MIN INT_MAX

...... Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 12 / 22 (#include )

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 )

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 )

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 )

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 )

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 ) typedef /*implementation -defined*/ size_t;

...... Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 16 / 22 (#include ) 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 )

scanf printf

getchar putchar

fopen fclose

EOF

...... Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 17 / 22 (#include )

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 ) 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 ) 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 ) 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 ) 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 )

...... 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