简体   繁体   English

传递带有变量参数的函数作为void指针,并调用它

[英]passing function with variable parameter as void pointer, and calling it

I have a function which I want to pass through various functions 我有一个功能,我想通过各种功能

int func1(char *ptr)
{
    printf("%s",ptr);
    return 0;
}

and other function in which I want to call the func1 和其他我想调用func1的函数

int func2(void *i)
{
    //call i
    //here i point to function func1
    //and do something with return value of i
}

So, how should I call it in main()? 那么,我应该如何在main()中调用它?

int main()
{
    void *d;
    //SOMETHING wrong in next four line
    d=&func1("abcd");
    func2(d);
    d=&func1("xyz");
    func2(d);
    return 0;
}

You can simply create a function ( func2 ) that takes a function pointer to your desired function that you want called and another paramter that takes a pointer to your string. 你可以简单地创建一个函数( func2 ),它接受一个函数指针,指向你想要调用的函数,另一个参数指向你的字符串。 Then in func2 call the passed in function pointer with the str argument. 然后在func2中使用str参数调用传入的函数指针。

#include <stdio.h>

void func1(const char *str)
{
    puts(str);
}

void func2(void (*fp)(const char *), const char *str)
{
    fp(str);
}

int main(void)
{
    const char *str = "Hello world.";

    func2(func1, str);

    return 0;
}

You should first of all use Function pointers to do the job you desire. 您首先应该使用函数指针来完成您想要的工作。 Using void pointer is a method where you before hand know what you will cast it to for dereferencing. 使用void指针是一种方法,您可以事先了解要将其转换为解除引用的内容。 Since you are passing the address of a function, you should cast it to a function pointer anyways. 由于您传递的是函数的地址,因此无论如何都应该将它转换为函数指针。 So avoid all this and declare func pointer anyways. 所以避免所有这些,并反正声明func指针。 Use void * only if you know that you can handle all the possiblities. 当您知道可以处理所有可能性时才使用void *。 eg. 例如。 memcpy uses void * this is acceptable as you just want a bunch of data in soure location to be copied to a destination location. memcpy使用void *这是可以接受的,因为您只需要将soure位置中的一堆数据复制到目标位置。

#include <stdio.h>
void func1(char * str)
{
   printf("%s",str);
}
void func2(void * g)
{
   void (*p) (char *);   //function pointer
   p = (void (*)(char *))g;//casting void * to pi.e func pointer reqiuired eitherways.
   p("Func pointer caller");
}
void main()
{
   void * d = func1; // name of the function is itselt its address
   //printf("func1 = %d\nd = %d",func1,d);  
   func2(d);
}

all this can be avoided by a simple function pointer. 所有这些都可以通过一个简单的函数指针来避免。

First of all: A function pointer refers to a function only. 首先:函数指针仅指一个函数。 Parameters come into the game when the function is called, either directly or via a function pointer. 调用函数时,参数直接或通过函数指针进入游戏。

So if you want to achieve what you have in mind, that is binding a (set of) parameters to a specific function varaible you need to use a wrapper function: 因此,如果您想实现您的想法,那就是将(一组)参数绑定到特定函数变量,您需要使用包装函数:

int func1(char * pc)
{
  int i;
  /* assigne somehting to "i" and use "pc" */
  return i;
}

int func1_abcd(void)
{
  return func1("abcd")
}

int func1_xyz(void)
{
  return func1("xyz")
}

typedef int (*pfunc1_wrapper_t)(void);

int func2(pfunc1_wrapper_t pfunc1_wrapper)
{
  return pfunc1_wrapper();
}

int main(int argc, char ** argv)
{
  pfunc1_wrapper_t pfunc1_wrapper = NULL;
  int i = 0;

  pfunc1_wrapper = func1_abcd;
  i = func2(pfunc1_wrapper);

  pfunc1_wrapper = func1_xyz;
  i = func2(pfunc1_wrapper);

  ...

  return 0;
}

请参阅此函数指针并阅读Wiki - Function Pointers ,您将了解如何修复代码。

Jatin already posted a general link to function pointers, very good explaining function pointers. Jatin已经发布了函数指针的一般链接,很好地解释了函数指针。

As you seam to want to pass not a classic function pointer, but a function pointer WITH it's argument as one "function pointer" to an other function, I do not think that this will work. 当你想要传递不是经典函数指针但是函数指针将它的参数作为一个“函数指针”指向另一个函数时,我不认为这会起作用。

But why passing a function with its argument as one parameter? 但为什么将一个带有参数的函数作为一个参数传递? This would mean, that the function you are passing with parameters, will return only one result, that is already known outside func2 (as func2 will call func1 with this already outside defined parameter set). 这意味着,您使用参数传递的函数将仅返回一个结果,该结果已在func2之外已知(因为func2将使用此已定义的外部参数集调用func1)。 So why not just pass the result of the func1 call to func2? 那么为什么不将func1调用的结果传递给func2呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM