I have this:
typedef void (*funcptr) (void);
int main(){
funcptr(); //this can compile and run with no error . WHAT DOES IT MEAN? WHY NO ERRORS?
}
I have this:
typedef void (*funcptr) (void);
int main(){
funcptr(); //this can compile and run with no error . WHAT DOES IT MEAN? WHY NO ERRORS?
}
You are defining a datatype, funcptr, which is a function that takes no parameters and returns void. You are then creating an instance of it, but without an identifier, discarding it.
In C++ language, any expression of the form some_type()
creates a value of type some_type
. The value is value-initialized.
For example, expression int()
creates a value-initialized value of type int
. Value-initialization for int
means zero initialization, meaning that int()
evaluates to compile-time integer zero.
The same thing happens in your example. You created a value-initialized value of type funcptr
. For pointer types, value-initialization means initialization with null-pointer.
(Note also, that it is absolutely incorrect to say that expressions like int()
, double()
or the one in your OP with non-class types use "default constructors". Non-class types have no constructors. The concept of initialization for non-class types is defined by the language specification without involving any "constructors".)
In other words, you are not really "playing with function pointer" in your code sample. You are creating a null function pointer value, but you are not doing anything else with it, which is why the code does not exhibit any problems. If you wanted to attempt a call through that function pointer, it would look as follows funcptr()()
(note two pairs of ()
), and this code would most certainly crash, since that's what usually happens when one attempts a call through a null function pointer value.