Hãy xem mã này:Tên chức năng hoặc con trỏ hàm?
#include <stdio.h>
typedef int (*callback) (void *arg);
callback world = NULL;
int f(void *_) {
printf("World!");
return 0;
}
int main() {
printf("Hello, ");
// world = f;
world = &f; // both works
if (world != NULL) {
world(NULL);
}
}
Khi cài đặt world
biến, cả world = f;
và world = &f;
công trình.
Tôi nên sử dụng thông tin nào? Liệu nó phụ thuộc vào trình biên dịch hoặc phiên bản C?
% gcc -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.0.0 (clang-800.0.38)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
http://stackoverflow.com/questions/6293403/in-c-what-is-the-difference-between-function-and-function-when-passed-as-a – Pavel
http: // stackoverflow .com/questions/3674200/what-do-a-typedef-với-dấu ngoặc đơn-như-typedef-int-fvoid-có nghĩa là-là-nó-a –