我在这里遇到了一些问题,我在搞乱机器代码和函数指针,而且我的一些代码 VC++ 只是拒绝编译。
这完全按预期编译和运行:
#include <stdlib.h>
#include <stdio.h>
int main()
{
char tarr[] = {0xb8, 222, 0, 0, 0, 0xc3};
int (*testfn)() = tarr;
printf("%d", testfn()); // prints 222
getchar();
}
但是,Visual C++ Express不会编译以下内容,并给出此错误:error C2143: syntax error : missing ';' before 'type'
#include <stdlib.h>
#include <stdio.h>
int main()
{
char* tarr = (char*) malloc(1000);
tarr[0] = 0xb8;
tarr[1] = 222;
tarr[2] = 0;
tarr[3] = 0;
tarr[4] = 0;
tarr[5] = 0xc3;
int (*testfn)() = tarr; // syntax error here
printf("%d", testfn());
getchar();
}
我查看了所谓的错误代码,我看不出它有什么问题。这是怎么回事?有什么我想念的吗?