标签:des style blog http color os io strong ar
#include <stdio.h>void show( char(*s)[10], int n){while(n--)printf("%s\n",s++);}int main(){char s[3][10] = {"hello","liuwei","xuanyuan"};show(s,3);return 0;}






#include <stdio.h>void show(char * s){printf("%s\n",s);return;}int main(){char str[] = "hello world";show(str);void(*p)(char *); //这两行代码可以直接写成下面的p = show; //void(*p)(char *) = show;p(str);return 0;}

#include <stdio.h>struct STU{int id;char name[20];char sex;void(*p)(void);};void st1(void){printf("I am st1\n");return;}void st2(void){printf("I am st2\n");return;}int main(void){struct STU a = { 10, "liuwei", ‘M‘, st1 };struct STU b = { 20, "xuanyuan", ‘W‘, st2 };printf("%d %s %c\n", a.id, a.name, a.sex);a.p();printf("%d %s %c\n", b.id, b.name, b.sex);b.p();return 0;}

函数指针就是c++的方法实现(php同理):
#include <stdio.h>#include <stdlib.h>typedef struct STU{int id;char name[20];char sex;void(*show)(pS);}S, *pS;void show(pS this){printf("%d %s %c\n", this->id, this->name, this->sex);}int main(void){S a = { 10, "liuwei", ‘M‘, show };a.show(&a);return 0;}
![]()
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <string.h>#define N 3typedef struct STU{int id;char name[20];char sex;int score;}S, *pS;void sort_id(pS a, int n){int i, j, min;S temp;for (i = 0; i < n; ++i){min = i;for (j = i + 1; j < n; ++j)if (a[min].id > a[j].id)min = j;temp = a[min];a[min] = a[i];a[i] = temp;}return;}void sort_name(pS a, int n){int i, j, min;S temp;for (i = 0; i < n; ++i){min = i;for (j = i + 1; j < n; ++j)if (strcmp(a[min].name, a[j].name) > 0)min = j;temp = a[min];a[min] = a[i];a[i] = temp;}return;}void sort_score(pS a, int n){int i, j, min;S temp;for (i = 0; i < n; ++i){min = i;for (j = i + 1; j < n; ++j)if (a[min].score > a[j].score)min = j;temp = a[min];a[min] = a[i];a[i] = temp;}return;}void init_stu(pS a, int n){a[0].id = 50;strcpy(a[0].name, "liuwei");a[0].score = 78;a[0].sex = ‘M‘;a[1].id = 60;strcpy(a[1].name, "xuanyu");a[1].score = 90;a[1].sex = ‘W‘;a[2].id = 10;strcpy(a[2].name, "hello");a[2].score = 63;a[2].sex = ‘M‘;}void show_stu(pS a, int n){int i;for (i = 0; i < n; ++i){printf("%d\t%s\t%c\t%d\n", a[i].id, a[i].name, a[i].sex, a[i].score);}}int main(void){int n;struct STU a[N];init_stu(a, N);show_stu(a, N);printf("请输入以下数字排序:\n0---sort_id\n1---sort_name\n2---sort_score\n");void(*fun[])(pS, int) = { sort_id, sort_name, sort_score }; //这是个函数指针数组scanf("%d", &n);while (n > sizeof(fun) / sizeof(fun[0]) - 1){printf("请输入以下数字排序:\n0---sort_id\n1---sort_name\n2---sort_score\n");scanf("%d", &n);}fun[n](a, N);show_stu(a, N);return 0;}

#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <stdlib.h>#include <string.h>#define N 3typedef struct STU{int id;char name[20];char sex;int score;}S, *pS;int cmp_name(pS a, pS b){return strcmp(a->name, b->name);}int cmp_score(pS a, pS b){return a->score - b->score;}int cmp_id(pS a, pS b){return a->id - b->id;}void sort_stu(pS a, int n, int(*cmp)(pS, pS)){int i, j, min;S temp;for (i = 0; i < n; ++i){min = i;for (j = i + 1; j < n; ++j)if (cmp(&a[min], &a[j]) > 0)min = j;temp = a[min];a[min] = a[i];a[i] = temp;}return;}void init_stu(pS a, int n){a[0].id = 50;strcpy(a[0].name, "liuwei");a[0].score = 58;a[0].sex = ‘M‘;a[1].id = 60;strcpy(a[1].name, "buanyu");a[1].score = 40;a[1].sex = ‘W‘;a[2].id = 10;strcpy(a[2].name, "hello");a[2].score = 63;a[2].sex = ‘M‘;}void show_stu(pS a, int n){int i;for (i = 0; i < n; ++i){printf("%d\t%s\t%c\t%d\n", a[i].id, a[i].name, a[i].sex, a[i].score);}}int main(void){int n;struct STU a[N];init_stu(a, N);show_stu(a, N);while (1){printf("请输入以下数字排序:\n0---sort_id\n1---sort_name\n2---sort_score\n");scanf("%d", &n);switch (n){case 0:sort_stu(a, N, cmp_id);break;case 1:sort_stu(a, N, cmp_name);break;case 2:sort_stu(a, N, cmp_score);break;}printf("**************\n");show_stu(a, N);}return 0;}

#include <stdio.h>#include <stdlib.h>void say(void){printf("hello world\n");}void fun(void(*f)(void)){f();}int main(){fun(say); //传递的是一个函数指针return 0;}

#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>#include <string.h>#define N 100struct STR{char * p;int n;};void show(struct STR * a, int n){int i;for (i = 0; i < n; i++){printf("%s-------%d\n", a[i].p, a[i].n);}printf("*************\n");return;}void sort_str(struct STR * a, int n, int(*fun)(struct STR *, struct STR *)){int i, j, min;struct STR temp;for (i = 0; i < n; i++){min = i;for (j = i + 1; j < n; j++)if (fun(&a[min], &a[j]) > 0)min = j;temp = a[min];a[min] = a[i];a[i] = temp;}return;}int cmp_dict(struct STR * a, struct STR * b){return strcmp(a->p, b->p);}int cmp_times(struct STR * a, struct STR * b){return a->n - b->n;}int cmp_len(struct STR * a, struct STR * b){return strlen(a->p) - strlen(b->p);}int main(){struct STR a[N];char str[] = "hello hello good say oh yeah hello say world xwp xwp hello";int m = 1, i = 0, flag = 0;char * p;p = strtok(str, " ");if (p == NULL)return 1;a[0].p = p;a[0].n = 1;while (p = strtok(NULL, " ")){flag = 0;for (i = 0; i < m; i++){if (strcmp(a[i].p, p) == 0){flag = 1;a[i].n += 1;break;}}if (!flag){a[m].p = p;a[m].n = 1;m++;}}show(a, m);sort_str(a, m, cmp_dict);show(a, m);sort_str(a, m, cmp_times);show(a, m);sort_str(a, m, cmp_len);show(a, m);return 0;}

标签:des style blog http color os io strong ar
原文地址:http://www.cnblogs.com/l6241425/p/3952245.html