# 一点也不神秘的函数的指针

2017-11-20 09:59:09来源:https://www.lijingquan.net/2017/11/19/一点也不神秘的函数的指针/作者:Tater人点击

#include <stdio.h>
int testfunc(int a)
{
printf("My a = %d", a);
}
int main()
{
int (*myfunc)(int) = testfunc;
myfunc(5);
return 0;
}
~

#include <stdio.h>
int test1(int a)
{
printf("My a = %d/n", a);
}
int test2(int b)
{
printf("My b = %d/n", b);
}
int main()
{
int (*myfunc[2])(int) = {test1, test2};
(myfunc[0])(5);
(myfunc[1])(5);
return 0;
}

#include <stdio.h>
int test1(int a, int b)
{
printf("My a = %d/n", a + b);
}
int test2(int c, int d)
{
printf("My b = %d/n", c * d);
}
int main()
{
int (*myfunc[2])(int, int) = {test1, test2};
(myfunc[0])(5, 4);
(myfunc[1])(5, 4);
return 0;
}

My a = 9

My b = 20

#include <stdio.h>
int test1(void *a, void *b)
{
printf("My a = %d/n", *(int *)a + * (int *)b);
}
int test2(void *c, void *d)
{
printf("My b = %d/n", *(int *)c * *(int *)d);
}
int main()
{
int val1 = 5;
int val2 = 10;
int (*myfunc[2])(void *, void *) = {test1, test2};
(myfunc[0])(&val1, &val2);
(myfunc[1])(&val1, &val2);
return 0;
}

#include <stdio.h>
int test1(const void *a, const void *b)
{
printf("My a = %d/n", *(int *)a + * (int *)b);
}
int test2(const void *c, const void *d)
{
printf("My b = %d/n", *(int *)c * *(int *)d);
}
int main()
{
int val1 = 5;
int val2 = 10;
int (*myfunc[2])(const void *, const void *) = {test1, test2};
(myfunc[0])(&val1, &val2);
(myfunc[1])(&val1, &val2);
return 0;
}

#include <stdio.h>
int main(int argc,char *argv[]){
}

int test1(const void *a, const void *b)
{
if(strcmp((*(char **)a, *(char **)b))
{
printf("a bigger than b/n");
}
else
{
printf("b bigger than a/n");
}
}

int test1(const void *a, const void *b)
{
if(strcmp((char *)a, (char *)b))
{
printf("a bigger than b/n");
}
else
{
printf("b bigger than a/n");
}
}