ueach是一个通过Unicode字符串循环的函数,通过向每个字符传递一个字符串来对每个字符运行回调。

string ueach(string s, void *function(string)) {
    unsigned long i;
    for (i = 0; i < s.length; i++)
        function(uchar(s, i));
}

如果我有回拨电话:
void testing(string c) {
    puts(utoc(c));
}

它打印给定的字符(testing将Unicode字符串转换为UTF-8utoc),一切正常使用的代码:
string a = ctou("Hello, world!");
ueach(a, &testing);

但是,我得到了这样的警告:
test.c: In function ‘main’:
test.c:8: warning: passing argument 2 of ‘ueach’ from incompatible pointer type
ulib:171: note: expected ‘void * (*)(struct string)’ but argument is of type ‘void (*)(struct string)’

如果我像这样在char *原型的函数部分加上括号:
string ueach(string s, void (*function)(string)) { ... }

然后它也工作得很好,没有任何警告。
ueachvoid * (*)(struct string)有什么区别?
void (*)(struct string)void *function(string)有什么区别?

最佳答案

void * (*)(struct string)-指向返回void *的函数的指针。
void (*)(struct string)-指向返回void的函数的指针。
void *function(string)-返回void *的函数
void (*function)(string)-指向返回void的函数的指针
第三个堕落为第一个,因为:
“除非它是
sizeof运算符或一元&
运算符,具有
类型“函数返回类型”是
转换为具有
type'指向函数返回的指针
键入“.”
C99第6.3.2.1/4节

07-24 09:51
查看更多