【发布时间】:2011-11-23 01:06:04
【问题描述】:
void myprint(char* x) {
printf("%s\n", x);
}
int main() {
char* s = "hello";
void (*test)(char*);
void (*test2)(char*);
test = myprint;
test2 = &myprint;
test(s);
(*test)(s);
test2(s);
(*test2)(s);
}
谁能向我解释为什么以上所有代码都有效? “你好”打印四次。通过应用函数指针,它是否被隐式取消引用?基本上我想知道函数指针实际上是如何存储的,因为上面有点令人困惑。
【问题讨论】:
-
(******test)(s)也是有效的 :-) -
函数和函数指针是特殊的,它们的处理与任何其他类型的指针都不统一。请参阅侧栏中的How does dereferencing of a function pointer happen? 和其他问题。
标签: c function-pointers