1.声明函数指针
int (*func1)(int x);
void (*func2)(double y, double z);
char (*func3)(char* p[]);
void (*func4)();
2.初始化函数指针及其用法
2.1用函数指针调用函数
#include <stdio.h>
//函数原型
double square(double x);
//声明函数指针
double (*ptr)(double x);
int main()
{
//让指针指向square()
ptr = square;
//用两种方式调用square()
printf("%f %f\n", square(6.6), ptr(6.6));
return 0;
}
double square(double x)
{
return x * x;
}
2.2用函数指针根据不同的情况调用不同的函数
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
//函数原型
void func1(int x);
void one(void);
void two(void);
void other(void);
int main()
{
int nbr;
for (;;)
{
puts("\nEnter an integer between 1 and 10, 0 to exit: ");
scanf("%d", &nbr);
if (nbr == 0)
{
break;
}
func1(nbr);
}
return 0;
}
void func1(int x)
{
//函数指针
void (*ptr)(void);
if (x == 1)
{
ptr = one;
}
else if (x == 2)
{
ptr = two;
}
else
{
ptr = other;
}
ptr();
}
void one(void)
{
puts("You entered 1.");
}
void two(void)
{
puts("You entered 2.");
}
void other(void)
{
puts("You entered smoething other than 1 or 2.");
}
2.3函数指针传递给函数
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
//函数原型
void func1(void (*p)(void));
void one(void);
void two(void);
void other(void);
int main()
{
void (*ptr)(void);
int nbr;
for (;;)
{
puts("\nEnter an integer between 1 and 10, 0 to exit: ");
scanf("%d", &nbr);
if (nbr == 0)
{
break;
}
else if (nbr == 1)
{
ptr = one;
}
else if (nbr == 2)
{
ptr = two;
}
else
{
ptr = other;
}
func1(ptr);
}
return 0;
}
void func1(void(*p)(void))
{
p();
}
void one(void)
{
puts("You entered 1.");
}
void two(void)
{
puts("You entered 2.");
}
void other(void)
{
puts("You entered smoething other than 1 or 2.");
}
2.4用函数指针控制排序的顺序
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXLINES 25
int get_lines(char* lines[]);
void sort(char* p[], int n, int sort_type);
void print_strings(char* p[], int n);
int alpha(char* p1, char* p2);
int reverse(char* p1, char* p2);
char* lines[MAXLINES];
int main()
{
int number_of_lines, sort_type;
number_of_lines = get_lines(lines);
if (number_of_lines < 0)
{
puts("Memory allocation error");
exit(-1);
}
puts("Enter 0 for reverse order sort, 1 for alphabetical:");
scanf("%d", &sort_type);
sort(lines, number_of_lines, sort_type);
print_strings(lines, number_of_lines);
return 0;
}
int get_lines(char* lines[])
{
int n = 0;
char buffer[80];
puts("Enter one line at time; enter a blank when done.");
while (n < MAXLINES && gets_s(buffer) != 0 && buffer[0] != '\0')
{
if ((lines[n] = (char*)malloc(strlen(buffer) + 1)) == NULL)
{
return -1;
}
strcpy(lines[n++], buffer);
}
return n;
}
void sort(char* p[], int n, int sort_type)
{
int a, b;
char* x;
int (*compare)(char* s1, char* s2);
compare = (sort_type) ? reverse : alpha;
for (a = 1; a < n; a++)
{
for (b = 0; b < n - 1; b++)
{
if (compare(p[b], p[b + 1]) > 0)
{
x = p[b];
p[b] = p[b + 1];
p[b + 1] = x;
}
}
}
}
void print_strings(char* p[], int n)
{
int count;
for (count = 0; count < n; count++)
{
printf("%s\n", p[count]);
}
}
int alpha(char* p1, char* p2)
{
return (strcmp(p2, p1));
}
int reverse(char* p1, char* p2)
{
return (strcmp(p1, p2));
}