【发布时间】:2018-05-30 20:26:09
【问题描述】:
我要动态声明函数指针并排序
#include <stdio.h>
#include <stdlib.h>
int values[] = { 88, 56, 100, 2, 25 };
int main () {
int n;
printf("Before sorting the list is: \n");
for( n = 0 ; n < 5; n++ ) {
printf("%d ", values[n]);
}
int (^comp)() = ^int(const void *a, const void *b) {
return ( *(int*)a - *(int*)b );
};
qsort(values, 5, sizeof(int), /*ERROR*/comp);
printf("\nAfter sorting the list is: \n");
for( n = 0 ; n < 5; n++ ) {
printf("%d ", values[n]);
}
return(0);
}
但我收到以下错误:
错误:将 'int (^)()' 传递给不兼容类型的参数 'int (* _Nonnull)(const void *, const void *)' qsort(values, 5, sizeof(int), /ERROR/comp);
注意:在此处将参数传递给参数“__compar” int (* _Nonnull __compar)(const void *, const void *));
【问题讨论】:
-
嗯,编译器的信息已经很清楚了。
-
是的,我不知道 Apple 是如何做到这些 ^block 的,但我猜该类型不兼容。不过这里不需要关闭
-
我认为您应该问的问题是:“我可以在这里将块转换为函数指针吗?”
-
我的问题的答案是“Not really”
-
Nvm,我找到了你问题的答案