I am trying to use typedef
with a pointer to function, and it's behaving weirdly.
The following code works:
#include <stdio.h>
#include <stdlib.h>
int funct(int num)
{
return 0;
}
typedef int(*func)(int num);
int main()
{
if (1) { return 0; }
func c = &funct;
return 0;
}
But the following code (without the {}
) doesn't:
#include <stdio.h>
#include <stdlib.h>
int funct(int num)
{
return 0;
}
typedef int(*func)(int num);
int main()
{
if (1) return 0;
func c = &funct;
return 0;
}
It tells me it's an illegal use of the function. Why? Isn't it basically the same code?
EDIT: Errors-
Warning 4 warning C4047: '=' : 'int' differs in levels of indirection from 'int (__cdecl *)(int)'
Error 1 error C2275: 'func' : illegal use of this type as an expression
Error 2 error C2146: syntax error : missing ';' before identifier 'c'
Error 3 error C2065: 'c' : undeclared identifier
EDIT 2: I inserted the complete code. In the first example, I get no error after I build the code. In the second piece of code, I get all the errors mentioned above.
EDIT 3: I created a new file and tried the code and it works. I still have no idea what caused the error in the first place, so if someone could give me an idea that would be great.