这是一个空指针吗?铸件?它在做什么?

发布于 2024-12-20 02:50:49 字数 154 浏览 6 评论 0原文

我是 C 语言和指针的新手,我对这个函数声明感到困惑:

void someFunction(int (*)(const void *, const void *));

任何人都可以用外行的术语解释它的作用以及它是如何工作的吗?

I am new to the C language and pointers and I am confused by this function declaration:

void someFunction(int (*)(const void *, const void *));

Can anyone explain in layman's terms what this does and how it works?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

生生漫 2024-12-27 02:50:49

它是一个函数的原型,它需要:

指向函数的指针,该函数接受 const void*const void* 作为参数并返回 int

作为参数,并返回 <代码>无效。

It's the prototype of a function that takes:

a pointer to a function that takes a const void* and a const void* as arguments and returns an int

as an argument, and returns void.

铁轨上的流浪者 2024-12-27 02:50:49

它声明一个函数,该函数接受另一个函数作为其参数,并且不返回任何内容。另一个函数将被声明为

int otherfunction( const void *, const void * );

,并且您将像这样调用 somefunction() :

somefunction( otherfunction );

It declares a function, which takes another function as its argument, and returns nothing. The other function would be declared as

int otherfunction( const void *, const void * );

and you would call somefunction() like this:

somefunction( otherfunction );
抽个烟儿 2024-12-27 02:50:49

它是一个具有单个参数的函数。该参数是一个指向函数的指针,该函数返回 int 并将这两个 void 指针指向常量数据参数。

It's a function that has a single parameter. That parameter is a pointer to a function that returns an int and takes those two void pointers to constant data parameters.

心房敞 2024-12-27 02:50:49

这是一个函数的声明,它采用函数指针作为其参数。最基本的形式是这样的:

void someFunction(argument_type);

其中 argument_typeint (*)(const void *, const void *),可以将其描述为“指针”一个带有两个 const void * 参数并返回 int 的函数”。即具有以下声明的任何函数:

int foo(const void *, const void *);

通过示例说明:

int foo_one(const void * x, const void * y) { ... }
int foo_two(const void * x, const void * y) { ... }

void someFunction(int (*)(const void *, const void *) function_ptr)
{
    const void * x = NULL;
    const void * y = NULL;
    int result;
    result = (*function_ptr)(x, y); // calls the function that was passed in
}

int main()
{
    someFunction(foo_one);
    someFunction(foo_two);
    return 0;
}

This is the declaration of a function which takes a function pointer as its argument. In its most basic form, it looks like this:

void someFunction(argument_type);

Where argument_type is int (*)(const void *, const void *), which can be described as a "pointer to a function that takes two const void * arguments, and returns an int". i.e. any function that has the following declaration:

int foo(const void *, const void *);

To illustrate by example:

int foo_one(const void * x, const void * y) { ... }
int foo_two(const void * x, const void * y) { ... }

void someFunction(int (*)(const void *, const void *) function_ptr)
{
    const void * x = NULL;
    const void * y = NULL;
    int result;
    result = (*function_ptr)(x, y); // calls the function that was passed in
}

int main()
{
    someFunction(foo_one);
    someFunction(foo_two);
    return 0;
}
一身软味 2024-12-27 02:50:49

检查此项在处理复杂声明时非常有帮助。

Check this very helpful when dealing with complex declarations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文