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?