我在這裏遇到了一些問題,我弄亂了機器代碼和函數指針,還有一點是我的代碼,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++快遞不會編譯如下,給這個錯誤: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();
}
我已經看過了據說錯誤的代碼,我看不出有什麼問題。這是怎麼回事?有什麼我失蹤?
with visual studio 2008第一個版本不編譯: 錯誤C2440:'初始化':無法從'char [6]'轉換爲'int(__cdecl *)(void)' – sergiom 2010-02-12 12:57:04
@sergiom這很奇怪 - 第一個代碼片段在VC++ 2008 Express中編譯得很好 – 2010-02-12 13:06:21