我如何使用功能指针 [英] How Do I Use Function Pointers

查看:73
本文介绍了我如何使用功能指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个enum



  typedef   enum  {
one = 1
two = 2
three = 3
} mynum ;





我有4种不同的功能funtwo ..有趣的如下



  void  funtwo(mynum * c,uint8_t * num)
{
int x =( int )num + c;
printf( num + c =%d \ n,x);
}
void funthree(uint8_t * a,uint8_t * b,uint8_t * c)
{
int x =( int )(* a + * b + * c);
printf( a + b + c =%d \ n,x);
}
void funfour(uint8_t * a,uint8_t * b,uint8_t * c,uint32_t * d)
{
int x =( int )* a +( int )* b +( int )* c +( int )* d;
printf( a + b + c + d =%d \ n, X);
}
void funfive(uint8_t * a,uint8_t * b,uint8_t * c,uint8_t * d,uint32_t * e)
{
int x =( int )* a +( int )* b +( int )* c +( int )* d +(< span class =code-keyword> int )* e;
printf( a + b + c + d =%d \ n, X);
}





现在如果我的枚举值为1那么我必须使用功能2

如果enum是2然后使用fun 3,4 enum是3我必须使用函数5



如果没有if else语句并使用函数指针我怎么做? br />


谢谢

解决方案

没有聪明的方法可以做到这一点,因为函数原型不兼容(你可能会使用开关,而不是 if 链。)



你可以重新安排你的功能,因为它们基本上对不同数量的参数做同样的事情,所以,一个功能就像



int sum(int v [],int v_items);



可以完成这项工作。


你的意思是这样的:



  typedef   void  funtwoPtr(mynum * c,uint8_t * num); 
typedef void funthreePtr(uint8_t * a,uint8_t * b,uint8_t * c);
/ *
...
...
相同for funfour和five * /



funtwoPtr * myFunTwo =& funtwo;
funthreePtr * myFunThree =& funthree;
/ *
...
...
相同for funfour和five * /


switch (eNum)
{
case one:
{
myFunTwo(& eNum,& numOne);
break ;
}
case 两个:
{
myFunThree(& numOne,& numTwo,& numThree);
break ;
}
case three->无论如何:
{
...
}
默认
{
break ;
}
}

/ * ------ -------------------------------------------------- ------- * /


Hi

I have a enum

typedef enum {
    one=1,
    two=2,
    three=3
}mynum;



and i have 4 different functions funtwo .. funfive as below

void funtwo(mynum *c, uint8_t *num )
{
   int x= (int)num+c;
   printf( "num + c = %d \n", x);
}
void funthree(uint8_t *a , uint8_t *b, uint8_t *c )
{
    int x= (int)(*a+*b+*c);
     printf( "a+b+ c = %d \n", x);
}
void funfour(uint8_t *a , uint8_t *b, uint8_t *c, uint32_t *d )
{
   int x= (int)*a+(int)*b+(int)*c+(int)*d;
     printf( "a+b+c+d = %d \n", x);
}
void funfive(uint8_t *a , uint8_t *b, uint8_t *c, uint8_t *d, uint32_t *e)
{
    int x= (int)*a+(int)*b+(int)*c+(int)*d+(int)*e;
     printf( "a+b+c+d = %d \n", x);
}



now if my enum value is 1 then i have to use function 2
if enum is 2 then use fun 3,4 enum is 3 i have to use function 5

How do i do this without if else statement and using function pointers?

Thanks

解决方案

There is no clever way for doing that, since function prototypes are not compatible (you might use a switch, instead of an if chain).

You could rearrange your functions, however, since they basically do the same thing on a different number of parameters, so, a single function like

int sum( int v[], int v_items);

could do the job.


Do you mean something like this:

typedef void funtwoPtr(mynum *c, uint8_t *num);
typedef void funthreePtr(uint8_t *a , uint8_t *b, uint8_t *c );
/* 
...
...
the same for funfour and five */


funtwoPtr* myFunTwo = &funtwo;
funthreePtr* myFunThree = &funthree;
/* 
...
...
the same for funfour and five */

switch (eNum)
{
   case one:
   {
      myFunTwo(&eNum, &numOne);
      break;
   }
   case two:
   {
      myFunThree(&numOne, &numTwo, &numThree);
      break;
   }
   case three->whatever:
   {
      ...
   }
   default:
   {
      break;
   }
}

/*---------------------------------------------------------------*/


这篇关于我如何使用功能指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆