简体   繁体   English

如何在C中调用具有void参数的Void函数

[英]How to call a Void function which has a void parameter in C

I am a beginner in C. For now I have a function like this 我是C语言的初学者。目前我有这样的功能

void mark_function(void *obj, void (*mark_obj)(void *));

To my understand, this a void function, and it has two parameter, first one is a void pointer, and the second one is a another void function with a void parameter. 据我了解,这是一个void函数,它有两个参数,第一个是void指针,第二个是另一个带有void参数的void函数。

I tried a lot ways to call it, but it seems not work properly,either give me back the segmentation false or the pointer type not the same warning. 我尝试了很多方法来调用它,但是它似乎无法正常工作,要么将分段错误返回给我,要么将指针类型警告不相同。

So What exactly means for this function? 那么这个功能到底意味着什么? what parameters should pass in? 应该传入什么参数? and how to call it? 以及如何称呼它? Any help? 有什么帮助吗?

Thanks alot! 非常感谢!

Let's make it more clear first. 首先让我们更清楚一点。 You have a void function, called mark_function , which takes 2 parameters. 您有一个称为mark_function的void函数,该函数mark_function 2个参数。 The first parameter is a void pointer and the second parameter is a pointer to a function that returns void and takes as a parameter a void pointer. 第一个参数是void指针,第二个参数是指向返回void并以void指针作为参数的函数的指针。 Let's create a function that will be apropiate to pass as parameter to mark_function . 让我们创建一个函数,该函数适合作为参数传递给mark_function

void param(void *p) {
    // function body
}

Assume a and b are 2 void pointers. 假设ab是2个void指针。 I will not enter into details about their scope, but you need to pay attention to it: they must be available in the scope they are used. 我不会详细介绍它们的范围,但是您需要注意:它们必须在所使用的范围内可用。 Then, the mark_function will be called as: 然后, mark_function将被称为:

mark_function(a, param);

Inside mark_function body you can have something like: mark_function主体内部,您可以执行以下操作:

param(b);

, which is a call to the function passed as a parameter. ,这是对作为参数传递的函数的调用。


Long story short: the function pointers used as parameters are meant to make it possible for the function that requires them to perform different activities by calling different functions. 长话短说:用作参数的函数指针旨在使要求它们通过调用不同的函数执行不同活动的函数成为可能。 The value of a pointer function is simply the name of a function that has the appropiate signature (return value and parameter list). 指针函数的值只是具有适当签名(返回值和参数列表)的函数的名称。 Your function can use this to call the function provided as parameter as needed. 您的函数可以根据需要使用它来调用作为参数提供的函数。

You should call the function with one pointer to an object, and one pointer to a function of suitable type. 您应该使用一个指向一个对象的指针和一个指向适当类型的函数的指针来调用该函数。

int my_int;

void mark_function(void *obj, void (*mark_obj)(void *));

void my_func (void *vp)
{
  /* Convert vp to a pointer to a real type */
  int *ip = vp;

  /* Do something with ip or *ip */
}

mark_function (&my_int, my_func);

A pointer to object ( &my_int ) can safely be convert to a void * and back to the same type. 可以将指向对象( &my_int )的指针安全地转换为void *并返回相同的类型。 The resulting pointer is guaranteed to be identical to the original pointer. 保证结果指针与原始指针相同。 This allows mark_function() to perform some task regardless of the actual type that the pointer points to. 这使mark_function()可以执行某些任务,而不管指针指向的实际类型是什么。

Typical examples of such tasks include sorting of arrays with a custom compare function provided by the caller, or maintaining linked lists without knowing the types of the object stored inside, an so on. 此类任务的典型示例包括使用调用方提供的自定义比较功能对数组进行排序,或者在不知道存储在内部的对象类型的情况下维护链表。

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

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