找不到我的语法错误,VC++说有一个
我在这里遇到了一些问题,我正在搞乱机器代码和函数指针,并且 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 不会编译以下内容,并给出此错误:错误 C2143:语法错误:缺少 ';'在“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();
}
我查看了所谓的错误代码,但看不出有什么问题。这是怎么回事?我有什么遗漏的吗?
I'm running into a bit of a problem here, I'm messing around with machine code and function pointers, and there's a bit of my code that VC++ simply refuses to compile.
This compiles and runs exactly as expected:
#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();
}
However, Visual C++ Express will not compile the following, giving this error: 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();
}
I've looked at the supposedly faulty code and I cannot see anything wrong with it. What's going on? Is there something I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是C代码吗?如果是这样,并且它不是 C99,那么您需要将 testfd 的声明移至 tarr[X] 的赋值之前。
Is this C code? If so, and it is not C99 then you need to move the declaration of testfd to before the assignments to tarr[X].
该代码在 GCC 中编译时会出现警告,而在 G++ 中则无法编译。你在那条线上缺少演员。您还缺少 main 的返回值。
The code compiles with warnings in GCC and fails to compile with G++. You're missing a cast on that line. You're also missing a return value from main.