Ngu*_*Dat 6 c syntax function-pointers
他们说这个表达式在C中是有效的,这意味着调用一个函数:
(*(void(*)())0)();
Run Code Online (Sandbox Code Playgroud)
有人可以清楚地解释这个表达的含义吗?
我试图编译它,并感到惊讶,它没有导致错误.
Ste*_*sop 18
一步步:
void(*)() // a pointer-to-function type, taking unspecified parameters
// and returning nothing.
(void(*)())0 // a null pointer of that pointer-to-function type
(*(void(*)())0) // dereference that pointer
(*(void(*)())0)(); // and call it with no parameters
Run Code Online (Sandbox Code Playgroud)
代码具有未定义的行为,它可能会因某种非法访问/段错误而崩溃.
您正在创建一个指向函数的指针,然后调用它.我不会称之为隐藏的功能,但未定义的行为.
基本上你这样做但是地址为0:
void test() { }
void(*pfn)() = test;
(*pfn)();
Run Code Online (Sandbox Code Playgroud)