1.函数类型
int Inc(int a)
{
return a+1;
}
int Add_Int(int a,int b)//函数的类型 返回类型+形参列表
{
return a+b;
}
int Mul_Int(int x,int y)
{
return x*y;
}
int main()
{
int fun(int,int);
int (*pfun)(int,int);
int (*pfun)(int,int)=NULL
return 0;
}
int Inc(int a)
{
return a+1;
}
int Add_Int(int a,int b)
return a+b;
}
int Mul_Int(int x,int y)
{
return x*y;
}
int main()
{
int x = 10, y = 20, z = 0;
Add_Int(x,y); // call
Add_Int; //不调用函数,函数的地址
}
3.函数指针
int Inc(int a)
{
return a+1;
}
int Add_Int(int a,int b)
return a+b;
}
int Mul_Int(int x,int y)
{
return x*y;
}
int main()
{
int x = 10, y = 20, z = 0;
int (*pfun)(int, int) = NULL;//函数地址
pfun = Add_Int;
pfun = Mul_Int;
sizeof(pfun); // 4
//sizeof(*pfun);
//pfun = pfun + 1; // void * ;
return 0;
}
4.调用
函数指针不能进行加一操作
int Inc(int a)
{
return a+1;
}
int Add_Int(int a,int b)
return a+b;
}
int Mul_Int(int x,int y)
{
return x*y;
}
int main()
{
int x = 10, y = 20, z = 0;
int (*pfun)(int, int) = NULL;
pfun = Add_Int;
z = (*pfun)(x, y);
z = pfun(x, y);
pfun = Mul_Int;
z = (*pfun)(x, y);
z = pfun(x, y);
return 0;
}
函数名不一样,但是返回类型和形参类型一样,就可以实现函数指针通用
5.函数指针作为形参使用
int Inc(int a)
{
return a+1;
}
int Add_Int(int a,int b)
return a+b;
}
int Mul_Int(int x,int y)
{
return x*y;
}
void Print(int x, int y, int (*pfun)(int, int))
{
if (pfun != NULL)
{
int z = pfun(x, y); // int z = (*pfun)(x,y);
printf("%d \n", z);
}
}
int main()
{
int x = 10, y = 20;
Print(x, y, Add_Int);
Print(x, y, Mul_Int);
return 0;
}
6.无类型函数指针通用性
void* Pint(const void* p)
{
int* ip = (int*)p;
printf("%d ", *ip);
return (ip + 1);
}
void* Pdouble(const void* p)
{
double* dp = (double*)p;
printf("%f ", *dp);
return (dp + 1);
}
void* Pchar(const void* p)
{
char* cp = (char*)p;
printf("%c ", *cp);
return (cp + 1);
}
void Print_Ar(void* ar, int n, void* (*fpun)(const void*))
{
for (int i = 0; i < n; ++i)
{
ar = fpun(ar);
}
printf("\n");
}
int main()
{
int ar[] = { 12,23,34,45,56,67 };
int in = sizeof(ar) / sizeof(ar[0]);
double dx[] = { 1.2,2.3,3.4,4.5,5.6,6.7,7.8,8.9 };
int dn = sizeof(dx) / sizeof(dx[0]);
char str[] = { 'a','k','a','s','h','i' };
int cn = sizeof(str) / sizeof(str[0]);
Print_Ar(ar, in, Pint);
Print_Ar(dx, dn, Pdouble);
Print_Ar(str, cn, Pchar);
return 0;
}
7.不同数据处理
void qsort(void* ptr,size_t count,size_t size,int ( * comp)(const void* ,const void *))
int IntComp(const void* arg1, const void* arg2)
{
const int* p1 = (const int* )arg1;
const int* p2 = (const int* )arg2;
if (*p1 > *p2) return 1;
else if (*p1 < *p2) return -1;
else return 0;
}
int DouComp(const void* arg1, const void* arg2)
{
const double* p1 = (const double* )arg1;
const double* p2 = (const double* )arg2;
if (*p1 > *p2) return 1;
else if (*p1 < *p2) return -1;
else return 0;
}
int main()
{
int ar[] = { 56,34,12,89,90,100,45,78,67 };
int in = sizeof(ar) / sizeof(ar[0]);
double dr[] = { 5.6,3.4,1.2,8.9,9.0,10.0,4.5,7.8,6.7 };
int dn = sizeof(dr) / sizeof(dr[0]);
qsort(ar, in, sizeof(int), IntComp);
qsort(dr, dn, sizeof(double), DouComp);
return 0;
}
字符串
int StrComp(const void* arg1, const void* arg2)
{
const char* sp1 = *(const char**)arg1;
const char* sp2 = *(const char**)arg2;
return strcmp(sp1, sp2);
}
int main()
{
const char* str[] = { "akashi","huming","newdata","print","const","wang" };
int n = sizeof(str) / sizeof(str[0]);
qsort(str, n, sizeof(char*), StrComp);
return 0;
}
结构体
struct Student
{
char s_name[16];
int s_age;
};
int StudComp(const void* arg1, const void* arg2)
{
const struct Student* sp1 = (const struct Student*)arg1;
const struct Student* sp2 = (const struct Student*)arg2;
//if (sp1->s_age > sp2->s_age) return -1;
//else if (sp1->s_age < sp2->s_age) return 1;
//else return 0;
return strcmp(sp2->s_name, sp1->s_name);
}
int main()
{
struct Student stud[] = { "akashi",19,"huming",23,"wang",16,"zhang",18,"linan",29 };
int n = sizeof(stud) / sizeof(stud[0]);
qsort(stud, n, sizeof(struct Student), StudComp);
return 0;
}
8.typedef与函数指针
int Add(int a, int b)
{
return a + b;
}
typedef int (*PFUN)(int, int);
int (*g_fun)(int, int) = NULL;
PFUN fun(PFUN p)
{
int* old = g_fun;
g_fun = p;
return old;
}
int main()
{
int x = 10, y = 20, z = 0;
z = Add(x, y);
z = (*(int (*)(int, int))0x00401020)(x, y);
z = ((PFUN)0x00401020)(x, y);
(*(void(*)())0)();
return 0;
}
int Add(int a, int b) { return a + b; }
int Mul(int a, int b) { return a * b; }
int Sub(int a, int b) { return a - b; }
typedef int(*PFUN)(int, int);
int main()
{
int (*pfun[3])(int, int) = { Add,Mul,Sub };
PFUN ar[3] = { Add,Mul,Sub };
return 0;
}