Asif wrote:
Hi there,
I have been trying to understand the behaviour of char (*pfn)(null)
for a couple of days. can some body help me understand the behaviour
of char (*pfn)(null) in Visual C++ environment?
The question is why this is legal
char *ptr;
char (*pfn)(null);
ptr = pfn
and this is not
char *ptr;
char (*fpn)();
ptr = *pfn // compile time error.
what actually is happening in the first case is that compiler treating
char (*pfn)(NULL) as char pointer. the question is why its not
generating the error for the first case too as both are two totally
different ideas.
Any response is highly appreciated.
Regards,
Asif
The declaration:
char (*pfn)(null);
is of a pointer to a function taking a "null" type and
returning a type of char. To my understanding, there is no
type of "null" in _standard_ C++.
Try this:
char (*pfn)(void);
A pointer to a function is different than a pointer to a variable.
One cannot assign the value from a "pointer to function" to a
"pointer to variable".
What you probably want is to assign the _result_ from the function
to the variable pointed to by a pointer:
char some_function(void)
{
return 'A';
}
char my_char;
char * ptr_my_char; // pointer to a char variable.
char (*pfn)(void); // pointer to a function.
ptr_my_char = &my_char; // initialize the variable pointer
pfn = some_function; // initialize the function pointer
*ptr_my_char = 'B'; // assign a char to 'my_char'
// via pointer. my_char == 'B'.
*ptr_my_char = (*pfn)(); // Execute some_function() via
// the pointer 'pfn' and put the
// result into 'my_char' via the
// pointer 'ptr_my_char'.
Remember that pointers to functions cannot be assigned to
pointers to variables.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq:
http://www.parashift.com/c++-faq-lite
C Faq:
http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book