数组指针 函数指针
数组指针的含义:
一个指针指向一个数组 ,这个指针+1就会加一个数组的长度。
#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;
}
指针数组与数组指针的区别:
char *s[] = { "hello", "world", "liuwei"}; //指针数组
内存地址如下:
S这里存储的是3个地址,即这个数组存了三个指针
然后该地址位置存储了常量字符串
char s[3][8] = { "hello", "world", "liuwei" }; //数组指针 s+1就相当于+8(列)个字节 二位数组等价于数组指针
内存地址如下:
函数指针的含义:
保存的是函数的地址
函数名的本质,表示此函数第一条指令的地址,可以说是函数的入口地址
#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;
}
请定义出以下函数的函数指针
1. int mystrlen(char *str)
2. char *mystrcpy(char *dest, char *src)
3. int mystrcmp(char *s1, char *s2)
4. int word_sort(char *s[], int n)
5. char *max_str(char *s[], int n, char **max)
1. int (*p)(char *);
2. char * (*p)(char *, char *);
3. int (*p)(char *, char *)
4. int (*p)(char **, int)
5. char * (*p)(char **, int, char **)
int (*p[10])(char *);函数指针数组
函数指针应用 :
#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;
}
函数指针数组实用案例:
通过输入不同的数字,对struct进行排序
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 3
typedef 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 3
typedef 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;
}
/*
习题:“hello good say oh yeah hello say world xwp xwp hello ...字符串处理
1.统计不重复单词出现的次数
2.按字典序对单词排序
3.按单词出现的频率从高到底排序
4.按单词的长度进行排序
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define N 100
struct 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;
}