Chinaunix首页 | 论坛 | 博客
  • 博客访问: 375839
  • 博文数量: 62
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 557
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-01 14:04
文章分类

全部博文(62)

文章存档

2014年(1)

2013年(61)

分类: C/C++

2013-12-19 14:58:12

原文地址:回调函数 _0302 作者:丫叩酱


点击(此处)折叠或打开

  1. #Makefile

  2. src = $(wildcard *.c)
  3. targets = $(patsubst %.c, %, $(src))

  4. CC = gcc
  5. CFLAGS = -Wall -g

  6. all:$(targets)
  7. $(targets):%:%.c
  8. $(CC) $(CFALGS) $< -o $@
  9. clean:
  10. -rm $(targets)
  11. .PHONY:clean all

点击(此处)折叠或打开

  1. //const 放在最左端,限定的是与const最接近的类型只读不可写
  2. //const不放在最左端,const具有左结合性,限定左边的只读不可写

  3. //const.c

  4. #include <stdio.h>

  5. const int n = 0;

  6. int main(int argc, const char *argv[])
  7. {
  8.     /*const*/ char * /*const*/p = "hello";

  9.     //p[0] = 'I';

  10.     printf("%s\n", p);
  11.     return 0;
  12. }

点击(此处)折叠或打开

  1. //const限定词,一定要注意:限定的类型,什么是只读的,什么是可读可写的

  2. //para_const.c

  3. #include <stdio.h>

  4. void str_handler(/*const*/ char *str)
  5. {
  6.     int i;
  7.     for (i = 0; str[i]; i++)
  8.     {
  9.         str[i] -= 32;
  10.     }
  11. }

  12. int main(int argc, const char *argv[])
  13. {
  14.     char str[] = "helloworld";

  15.     str_handler(str);

  16.     printf("%s\n", str);
  17.     return 0;
  18. }

点击(此处)折叠或打开

  1. //函数可返回的数据类型:
  2. // 传入变量的地址可返回
  3. // 静态局部变量的地址可返回
  4. // 动态申请的空间的首地址可返回

  5. //function_return_type.c

  6. #include <stdio.h>

  7. char *mystrcpy(char *dest, const char *src)
  8. {
  9.     static char str[50];
  10.     int i;

  11.     if(dest == NULL)
  12.         dest = str;
  13.     for (i = 0; (dest[i] = src[i]); i++)
  14.             ;
  15.     return dest;
  16. }

  17. int main(int argc, const char *argv[])
  18. {
  19.     char *p = NULL;
  20.     printf("%s\n", mystrcpy(p, "hello world"));
  21.     return 0;
  22. }

点击(此处)折叠或打开

  1. //双重指针的使用

  2. //getmemory.c

  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>

  6. void getmemory(char **pp)
  7. {
  8.     *pp = malloc(10);
  9. }

  10. int main(int argc, const char *argv[])
  11. {
  12.     char *p;

  13.     getmemory(&p);

  14.     strcpy(p, "hello");

  15.     printf("%s\n", p);
  16.     return 0;
  17. }

点击(此处)折叠或打开

  1. //1.#把函数指针当作函数的参数去用,被调的函数称为回调函数。如printstr、printnum函数

  2. //callback.c

  3. #include <stdio.h>

  4. typedef void (*fun_p)(void *);

  5. void printstr(void *str)
  6. {
  7.     printf("%s\n", (char *)str);
  8. }

  9. void printnum(void *num)
  10. {
  11.     printf("%d\n", (int)num);
  12. }

  13. void printmsg(fun_p fp, void *arg)
  14. {
  15.     fp(arg);
  16. }

  17. int main(int argc, const char *argv[])
  18. {
  19.     printmsg(printstr, (void *)"hello");
  20.     printmsg(printnum, (void *)123);
  21.     return 0;
  22. }

点击(此处)折叠或打开

  1. //2.#函数指针的使用
  2. //指针的赋值!!!!!!!

  3. //function_prt.c

  4. #include <stdio.h>

  5. typedef int (*func_t)(int, int);

  6. int add(int a, int b)
  7. {
  8.     return a + b;
  9. }

  10. int main(int argc, const char *argv[])
  11. {
  12.     func_t fp;
  13.     
  14.     fp = add;

  15.     printf("add: %d\n", fp(1, 2));
  16.     return 0;
  17. }

点击(此处)折叠或打开

  1. //3.函数指针的使用方法。在函数声明中将函数名的位置替换为函数指针,即为函数指针的定义

  2. //function_ptr_use.c

  3. #include <stdio.h>

  4. int add(int a, int b)
  5. {
  6.     return a + b;
  7. }

  8. int sub(int a, int b)
  9. {
  10.     return a-b;
  11. }

  12. int main(int argc, const char *argv[])
  13. {
  14.     int (*p)(int, int);

  15.     printf("sizeof(p) = %d\n", sizeof(p));
  16.     
  17.     p = add;
  18.     printf("add: %d\n", (*p)(1, 2));

  19.     p = sub;
  20.     printf("sub: %d\n", p(1, 2));

  21.     return 0;
  22. }

点击(此处)折叠或打开

  1. //4.注意变参函数的变参的调用顺序和压栈情况

  2. //get_a_day.c

  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>

  6. static const char *msg[]={"Sunday", "Monday", "Tueday", "Wednesday", "Thursday", "Friday", "Saterday"};

  7. char *get_a_day(int idx)
  8. {
  9.     /*static char buf[20];*/

  10.     char *buf = malloc(20);

  11.     strcpy(buf, msg[idx]);

  12.     printf("buf = %s\n", buf);

  13.     return buf;
  14. }

  15. int main(int argc, const char *argv[])
  16. {
  17.     printf("%s %s\n", get_a_day(0), get_a_day(1));
  18.     return 0;
  19. }

点击(此处)折叠或打开

  1. //泛型函数的使用,一定要注意:泛型函数的实现函数参数类型的转换

  2. //max_callback.c

  3. #include <stdio.h>
  4. #include <string.h>

  5. typedef int (*cmp_t)(void *, void *);
  6. typedef struct student
  7. {
  8.     char name[10];
  9.     int score;
  10. }std_t;

  11. int cmp_int(void *a, void *b)
  12. {
  13.     return *(int *)a - *(int *)b;
  14. }

  15. int cmp_str(void *dest, void *src)
  16. {
  17.     return strcmp(*((char **)dest), *((char **)src));
  18. }

  19. int cmp_std(void *a, void *b)
  20. {
  21.     return ((std_t *)a)->score - ((std_t *)b)->score;
  22. }

  23. void *max(void *base, int len, int size, cmp_t cmp)
  24. {
  25.     char *baseaddr = base, *maxp = base;
  26.     int i;

  27.     for (i = 1; i < len; i++)
  28.     {
  29.         if(cmp(maxp, baseaddr + i*size) < 0)
  30.             maxp = baseaddr + i*size;
  31.     }

  32.     return (void *)maxp;
  33. }

  34. int main(int argc, const char *argv[])
  35. {
  36.     int num[5], i;
  37.     int max_int;
  38.     
  39.     srand(time(NULL));
  40.     printf("array:");
  41.     for (i = 0; i < 5; i++)
  42.     {
  43.         num[i] = rand()%100 + 1;
  44.         printf("%4d", num[i]);
  45.     }
  46.     printf("\n");

  47.     max_int = *((int *)max(num, 5, 4, cmp_int));
  48.     printf("max_int: %d\n", max_int);


  49.     char *str[5] = {"hello", "aka", "world", "edu", "name"};
  50.     char **max_str;

  51.     max_str = max(str, 5, 4, cmp_str);
  52.     printf("max_str: %s\n", *max_str);


  53.     std_t stud[5] = {{"name1", 54}, {"name2", 65},{"name3", 87}, {"name4", 78}, {"name5", 56}};
  54.     std_t *s;

  55.     s = (std_t *)max(stud, 5, sizeof(std_t), cmp_std);
  56.     printf("max_std: %s %d\n", s->name, s->score);
  57.     return 0;
  58. }

点击(此处)折叠或打开

  1. //函数指针与结构体,结构体数组。
  2. //连续使用多个scanf时,一定要注意:读入缓冲区的有效字符。
  3. //此处的getchar不可省略,用来接收读入缓冲区中的垃圾字符

  4. //caculator_callback.c

  5. #include <stdio.h>

  6. #define NUM 4

  7. typedef int (*fun_t)(int, int);

  8. typedef struct operation
  9. {
  10.     fun_t fp;
  11.     char ch;
  12. }oper_t;


  13. int add(int a, int b)
  14. {
  15.     return a + b;
  16. }

  17. int sub(int a, int b)
  18. {
  19.     return a - b;
  20. }

  21. int mul(int a, int b)
  22. {
  23.     return a * b;
  24. }

  25. int mydiv(int a, int b)
  26. {
  27.     return a / b;
  28. }


  29. int main(int argc, const char *argv[])
  30. {
  31.     char c;
  32.     int a, b, i;

  33.     oper_t oper[NUM] = {{add, '+'}, {sub, '-'}, {mul, '*'}, {mydiv, '/'}};

  34.     while(1)
  35.     {
  36.         printf("operation:");
  37.         scanf("%c", &c);

  38.         if(c == '#')
  39.             break;

  40.         printf("input a b:");
  41.         scanf("%d %d", &a, &b);
  42.         
  43.         getchar();

  44.         for (i = 0; i < NUM; i++)
  45.         {
  46.             if(c == oper[i].ch)
  47.             {
  48.                 printf("result: %d\n", oper[i].fp(a, b));
  49.                 break;
  50.             }
  51.         }
  52.     }
  53.     return 0;
  54. }

阅读(2526) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~