...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
Function Pointer and C Standard Library
魏恒峰
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
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
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
SortCompare & 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 typesCompare & 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 typesCompare & 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 typesCompare & Swap
sort_ints
sort_floats
sort_strings
sort_persons
· · ·
Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 3 / 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
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
(#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 (*fptr)(int); // fptr is a function pointer
int square(int num) {return num * num;
}
int n = 5;fptr = square; // fptr points to a functionfptr(n); // call ''square''
(fptr-square.c)
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 functionfptr(n); // call ''square''
(fptr-square.c)
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 functionfptr(n); // call ''square''
(fptr-square.c)
Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 5 / 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
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
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
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
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
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
(#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 <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>)
isdigitisalphaisalnum
islowerisupper
isspace
tolowertoupper
Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 11 / 22
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
(#include <limits.h>)
CHAR_MINCHAR_MAX
INT_MININT_MAX
Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 12 / 22
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
(#include <math.h>)
sincos
explog
powsqrt
ceilfloor
Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 13 / 22
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
(#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
va_list // type
va_startva_argva_end
(miniprintf.c)
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(2) # unknown
(3) “· · · ” must be at the end(4) ≥ 1 named argument
va_list // type
va_startva_argva_end
(miniprintf.c)
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(2) # unknown
(3) “· · · ” must be at the end(4) ≥ 1 named argument
va_list // type
va_startva_argva_end
(miniprintf.c)
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(2) # unknown
(3) “· · · ” must be at the end(4) ≥ 1 named argument
va_list // type
va_startva_argva_end
(miniprintf.c)
Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 14 / 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
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
(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
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
(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
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
(#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 <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>)
scanfprintf
getcharputchar
fopenfclose
EOF
Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 17 / 22
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
(#include <stdlib.h>)
atoiatof
srandrand
mallocfree
bsearchqsort
Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 18 / 22
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
(#include <string.h>)
char book[] = ''The C Book'';char *pbook = ''The C Book'';
‘\0’
strncopystrncatstrncmp
strlen
strchrstrrchrstrstr
strtok
Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 19 / 22
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
(#include <string.h>)
char book[] = ''The C Book'';char *pbook = ''The C Book'';
‘\0’
strncopystrncatstrncmp
strlen
strchrstrrchrstrstr
strtok
Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 19 / 22
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
(#include <string.h>)
char book[] = ''The C Book'';char *pbook = ''The C Book'';
‘\0’
strncopystrncatstrncmp
strlen
strchrstrrchrstrstr
strtok
Hengfeng Wei ([email protected]) Function Pointer and C Standard Library 2017 年 11 月 24 日 19 / 22
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
...
.
(#include <string.h>)
char book[] = ''The C Book'';char *pbook = ''The C Book'';
‘\0’
strncopystrncatstrncmp
strlen
strchrstrrchrstrstr
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