Article ID: 104243
Article Last Modified on 7/5/2005
TYPE ( identifier ) ( expression );is used to type cast a function pointer and make a call simultaneously.
typedef void (* funcptr) (int);...
void f(int){...};
...
funcptr (f)(1); // Error C2440the following error message is generated: Although the last statement appears to be typecasting the function "f" to funcptr, the compiler thinks it is declaring an object of name "f" of type funcptr and initializing it with the integer constant "1" equivalent to having
funcptr f = 1;-or-
void (*f) (int) = 1;which makes the error message much more understandable.
((funcptr)f)(1);which resolves the confusion for the compiler, which thus treats the typecast in the way it should.
void (*f1)(int) = f;and use:
f1(1); // OK
typedef void (*funcptr) (int);
void f(int);
void main()
{
((void (*) (int)) f)(1);
funcptr (f) (1); // Gives error C2440
((funcptr)f)(1);
void (*f1) (int) = f;
f1(1);
}
Additional query words: 1.00 1.50 2.00 2.10 4.00 7.00 8.00 8.00c 9.00 9.10
Keywords: kbprb KB104243