one simple question

A

Asif

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
 
T

Thomas Matthews

Asif said:
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
 
A

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++.

You have the same kind of concept that i had :) Look what john said in
the same thread.

null = NULL; // sorry typo mistake.

char (*pfn)(NULL);
is not a pointer to a function taking a "null" type and
returning a type of char. Rather its just equivalent to char *pfn =
NULL;

I came to this discussion because i been reviewing one of my colleague
code and have found this discrepancy, He actually loved initializing
normal pointers with NULL the way i mentioned above and It looked
confusing at that time. But now its cleared.


Asif
 
F

Faz

Thomas Matthews said:
Asif wrote: []
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?
[]

You'll most likely find a macro called NULL in one of the Visual C++
headers.
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++.

Yes, there is no such type..
No, this doesn't declare a function.

It *would* if "null" were replaced by, say, "float" (which IS a type), or by
"double x" (a type and a name).
Empty brackets would be equivalent to replacing the "null" with "void", ie
also making this a function declaration (like your example later).

The NULL macro often supplied with some compilers means the brackets would
contain a value, hence this would declare a simple char* whose value is 0,
if the OP had uppercased it.
(As it stands its a syntax error :)

BTW Stroustrup (5.1.1) discourages use of a NULL macro and recommends
instead to use the literal 0 to indicate that the pointer doesn't refer to
an object (leads to fewer problems).

[]
One cannot assign the value from a "pointer to function" to a
"pointer to variable".

Not without coercion via an evil cast, true.

("Trust the programmer"..)

-Faz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top