C语言 void * function(void * argument),如何返回函数结果?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21708566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:45:28  来源:igfitidea点击:

void * function(void * argument), how to return the function results?

cfunctionpointers

提问by TonyGW

I am practicing for better understanding functions that return voidpointers: void *function_t(void *arg1, void *arg2);

我正在练习更好地理解返回void指针的函数:void *function_t(void *arg1, void *arg2);

I wrote the following code. Inside the function, I take two void *arguments and cast them to int *for summation. After getting the sum, I will return the voidpointer to the sum. But my code is not working. I got the following errors:

我写了以下代码。在函数内部,我使用两个 void *arguments 并将它们转换int *为求和。得到总和后,我将返回void总和的指针。但是我的代码不起作用。我收到以下错误:

pointerFuncTest2.c: In function ‘main':
pointerFuncTest2.c:23:11: error: lvalue required as left operand of assignment
pointerFuncTest2.c:27:5: error: invalid type argument of unary ‘*' (have ‘int')
pointerFuncTest2.c:28:43: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
pointerFuncTest2.c:28:5: error: too many arguments to function ‘vfunc'
pointerFuncTest2.c:22:11: note: declared here

The code:

编码:

  1 #include <stdio.h>
  2 
  3   void *func_t(void *arg1, void *arg2)
  4 {
  5         int *v;
  6         *v = *(int *) arg1;
  7         int *w;
  8         *w = *(int *) arg2;
  9 
 10         int *sum;
 11         *sum = *v + * w;
 12 
 13         return (void *) sum;
 14 }
 15 
 16 
 17 
 18 
 19 
 20   int main(void)
 21   {
 22     void* vfunc(void *);
 23     vfunc = func_t;
 24 
 25     int* value1, value2;
 26     *value1 = 100;
 27     *value2 = 2;
 28     void* sumvoid = vfunc((void *)value1, (void *)value2);
 29 
 30     int *sum;
 31     *sum = *(int *) sumvoid;
 32 
 33 
 34     printf("the value of sum is now %i\n", *sum);
 35 
 36     return 0;
 37   }

回答by David Heffernan

To fix the compilation error, you need to make the function pointer signature match that of the function:

要修复编译错误,您需要使函数指针签名与函数的签名匹配:

void* (*vfunc)(void *, void*);
vfunc = func_t;

You have bigger problems though. You declared pointers, but did not initialize them to point to anything. For instance:

你有更大的问题。您声明了指针,但没有将它们初始化为指向任何东西。例如:

int* value1, value2; // value1 and value2 are declared, but not initialized
*value1 = 100; // undefined behaviour because value1 is not initialized
*value2 = 2; // likewise

So you do need to make sure that your pointers are initialized. For instance:

所以你需要确保你的指针被初始化。例如:

int i;
int *p = &i;
*p = 42; // i == 42 at this point

In the case of a function that returns void*you will need to allocate memory dynamically. Like this:

对于返回的函数,void*您将需要动态分配内存。像这样:

int *retval = malloc(sizeof(int));
*retval = 666;
return retval;

Of course, void*is what you use when you don't know what type of object the pointer refers to. If you know what it is, then use that type.

当然,void*当你不知道指针指向什么类型的对象时,你会使用它。如果您知道它是什么,请使用该类型。

Having said all this, your entire code is something of a travesty. You are using pointers where pointers are not needed. You are using void*for seemingly no good reason.

说了这么多,你的整个代码有点讽刺。您在不需要指针的地方使用指针。您void*似乎没有充分理由使用。



Responding to your comment, you seem to want to write code like this:

回应你的评论,你似乎想写这样的代码:

void *func_t(int i1, int i2)
{
    int *retval = malloc(sizeof(int));
    *retval = i1 + i2;
    return retval;
}

The caller is now responsible for calling free()on the returned pointer. And real code would check for errors when calling malloc.

调用者现在负责调用free()返回的指针。真正的代码会在调用时检查错误malloc

I really don't know why you'd want to pass the parameters as void*, but suppose that's a reasonable thing to do. Your code would be:

我真的不知道您为什么要将参数作为 传递void*,但假设这是合理的做法。您的代码将是:

void *func_t(void *i1, void *i2)
{
    int *retval = malloc(sizeof(int));
    *retval = *(int*)i1 + *(int*)i2;
    return retval;
}

The complete program would look like this:

完整的程序如下所示:

#include <stdio.h>
#include <stdlib.h>

void *func_t(void *i1, void *i2)
{
    int *retval = malloc(sizeof(int));
    *retval = *(int*)i1 + *(int*)i2;
    return retval;
}

int main(void)
{
    void* (*vfunc)(void *, void*);
    vfunc = func_t;

    int value1 = 100;
    int value2 = 2;
    void* sumvoid = vfunc(&value1, &value2);
    int sum = *(int*)sumvoid;
    free(sumvoid);
    printf("the value of sum is now %d\n", sum);
    return 0;
}

It's still pretty weird mind you!

想想还是挺奇怪的!

回答by alk

To fix the compilation errors do:

要修复编译错误,请执行以下操作:

int main(void)
{
  void * (*vfunc)(void *, void *);
  vfunc = func_t;

  int * value1, * value2;
  *value1 = 100; /* This will provoke undefined behaviour as value1 is being 
                    dereferenced without having being initialised before. 
                    If then trying to write something to the where value1 is pointing
                    to, it shall point to some allocated memory. */
  *value2 = 2;   /* Same issue here as for value1. */
  void * sumvoid = vfunc(value1, value2);

Whether this code will do what you expect, or even run is a different story though.

这段代码是否会按照您的预期运行,甚至运行是另一回事。

回答by Edouard Thiel

In the whole example, using void*is completely useless.

在整个示例中, usingvoid*完全没有用。

You may write

你可以写

int *v = arg1;

without casting since arg1is a void*, but

不强制转换,因为arg1是 a void*,但是

int *v;
*v = ...

is wrong since vis not initialized.

是错误的,因为v没有初始化。

If you eventually want to return a void*without allocating memory, you may declare a static int (not thread safe) :

如果您最终想在void*不分配内存的情况下返回 a ,您可以声明一个静态 int(非线程安全):

static int res = ...
return &res;

Another error line 25 : add a *to value2:

另一个错误行 25:添加*value2

int *value1, *value2;