定义返回函数指针的函数有哪些不同的风格

发布于 2024-12-11 01:08:42 字数 163 浏览 0 评论 0原文

我有一个函数:

int compare(char * c1, char * c2){
...
...
}

我可以使用哪些不同的样式来编写返回要比较的指针的函数 int ret_compare(void * item)

I have one function:

int compare(char * c1, char * c2){
...
...
}

What are the various styles in which I can write a function int ret_compare(void * item) that returns a pointer to compare?

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

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

发布评论

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

评论(1

英雄似剑 2024-12-18 01:08:42

有两种主要样式,一种使用 typedef,一种不使用(有 typedef 的两种变体)。您的比较器应该采用常量指针,如下所示:

int compare(const char *c1, const char *c2) { ... }

// Raw definition of a function returning a pointer to a function that returns an int
// and takes two constant char pointers as arguments
int (*ret_compare1(void *item))(const char *, const char *)
{
    // Unused argument - item
    return compare;
}

// More usual typedef; a Comparator2 is a pointer to a function that returns an int
// and takes two constant char pointers as arguments
typedef int (*Comparator2)(const char *, const char *);

// And ret_compare2 is a function returning a Comparator2
Comparator2 ret_compare2(void *item)
{
    // Unused argument - item
    return compare;
}

// Less usual typedef; a Comparator3 is a function that returns an int
// and takes two constant char pointers as arguments
typedef int Comparator3(const char *, const char *);

// And ret_compare3 is a function returning a pointer to a Comparator3
Comparator3 *ret_compare3(void *item)
{
    // Unused argument - item
    return compare;
}

请注意,这些比较器不能与 bsearch()qsort() 一起使用(除非您使用相当可怕的强制转换),因为这些比较器预计采用 const void * 参数。

另请注意,为了比较字符串(而不是单个字符),qsort()bsearch() 使用的函数应类似于:

int string_comparator(const void *v1, const void *v2)
{
    const char *s1 = *(char **)v1;
    const char *s2 = *(char **)v2;
    return(strcmp(s1, s2));
}

There are two main styles, one using a typedef and one not (with two variants of the typedef). Your comparator should take constant pointers, as below:

int compare(const char *c1, const char *c2) { ... }

// Raw definition of a function returning a pointer to a function that returns an int
// and takes two constant char pointers as arguments
int (*ret_compare1(void *item))(const char *, const char *)
{
    // Unused argument - item
    return compare;
}

// More usual typedef; a Comparator2 is a pointer to a function that returns an int
// and takes two constant char pointers as arguments
typedef int (*Comparator2)(const char *, const char *);

// And ret_compare2 is a function returning a Comparator2
Comparator2 ret_compare2(void *item)
{
    // Unused argument - item
    return compare;
}

// Less usual typedef; a Comparator3 is a function that returns an int
// and takes two constant char pointers as arguments
typedef int Comparator3(const char *, const char *);

// And ret_compare3 is a function returning a pointer to a Comparator3
Comparator3 *ret_compare3(void *item)
{
    // Unused argument - item
    return compare;
}

Note that these comparators cannot be used with bsearch() and qsort() (unless you use fairly gruesome casts) because those comparators are expected to take const void * arguments.

Note, too, that for comparing strings, as opposed to single characters, the function used by qsort() or bsearch() should be similar to:

int string_comparator(const void *v1, const void *v2)
{
    const char *s1 = *(char **)v1;
    const char *s2 = *(char **)v2;
    return(strcmp(s1, s2));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文