69,382
社区成员
发帖
与我相关
我的任务
分享
int LocalElem(compare(),*e);
用e来存储第一个满足compare()的函数
typedef int (*COMPARE)()
typedef int *COMPARE()
int LocalElem(COMPARE c,int *e);
int LocalElem(compare, e);
#include <stdio.h>
int add(int a,int b)
{
return a+b;
}
int sub(int a,int b)
{
return a-b;
}
int (*fun)(int a,int b);
int calc(int a,int b,int (*fun)(int a,int b))
{
return fun(a,b);
}
int main(void)
{
int num1,num2;
char op;
scanf("%d %d %c",&num1,&num2,&op);
switch(op)
{
case '+':
fun=add;
break;
case '-':
fun=sub;
break;
default:
fun=add;
}
printf("%d\n",calc(num1,num2,fun));
return 0;
}
Example
/* QSORT.C: This program reads the command-line
* parameters and uses qsort to sort them. It
* then displays the sorted arguments.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int compare( const void *arg1, const void *arg2 );
void main( int argc, char **argv )
{
int i;
/* Eliminate argv[0] from sort: */
argv++;
argc--;
/* Sort remaining args using Quicksort algorithm: */
qsort( (void *)argv, (size_t)argc, sizeof( char * ), compare );
/* Output sorted list: */
for( i = 0; i < argc; ++i )
printf( "%s ", argv[i] );
printf( "\n" );
}
int compare( const void *arg1, const void *arg2 )
{
/* Compare all of both strings: */
return _stricmp( * ( char** ) arg1, * ( char** ) arg2 );
}
Output
[C:\code]qsort every good boy deserves favor
boy deserves every favor good