如何调用这个函数? [英] How to invoke this function?

查看:51
本文介绍了如何调用这个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Apple 文档中看到的以下示例,对如何调用 qsort_b 功能感到困惑?const void *l & 需要传递哪些参数?const void *r.

Below sample i saw in Apple document, confused about how to call qsort_b fucntion? What are the parameters need to pass as const void *l & const void *r.

char *myCharacters[3] = { "TomJohn", "George", "Charles Condomine" };
qsort_b(myCharacters, 3, sizeof(char *), ^(const void *l, const void *r) {
    char *left = *(char **)l;
    char *right = *(char **)r;
    return strncmp(left, right, 1);
})

推荐答案

根据文档,来自标准 C 库的 qsortqsort_b 的区别在于 <代码>qsort_b 需要一个 ,而不是函数指针:

According to the documentation, the difference between qsort from the standard C library and qsort_b is that qsort_b takes a block, rather than a function pointer:

void qsort(void *base, size_t nel, size_t width, int (*compar)(const void *, const void *));

void qsort_b(void *base, size_t nel, size_t width, int (^compar)(const void *, const void *));

块的用途与比较函数的用途相同——判断数组的两个元素是否相等,如果不相等,则哪一个大于另一个.

The purpose of the block is the same as that of the comparison function - determining whether the two elements of the array are equal, or if they are not, which one is greater than the other.

const void *l 需要传递哪些参数 &const void *r?

What are the parameters need to pass as const void *l & const void *r?

不是您传递这些参数:这些参数由 qsort_b 传递回您的比较块.第一个参数是被比较的左边元素的值;第二个是右边的元素.

It's not you who pass these parameters: these parameters are passed back to your comparison block by qsort_b. The first parameter is the value of the left element being compared; the second one is the element on the right.

这篇关于如何调用这个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆