By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,679 Members | 1,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,679 IT Pros & Developers. It's quick & easy.

one simple question

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

Jul 19 '05 #2

P: n/a
Well john, thanks for the reply.
Its cleared now.

Asif
"John Harrison" <jo*************@hotmail.com> wrote in message news:<bi************@ID-196037.news.uni-berlin.de>...
"Asif" <as******@hotmail.com> wrote in message
news:3e**************************@posting.google.c om...
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);


You mean 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


char (*pfn)(NULL);

is equivalent to

char (*pfn)(0);

is equivalent to

char *pfn(0);

is equivalent to

char *pfn = 0;

It's easy to forget that you can have redundant parens in variable
declarations. Try this

char ((((*pfn))))(NULL);

john

Jul 19 '05 #3

P: n/a
> 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
Thomas Matthews <Th**********************@sbcglobal.net> wrote in message news:<ZA**********************@newssvr28.news.prod igy.com>... 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

Jul 19 '05 #4

P: n/a
Faz
"Thomas Matthews" <Th**********************@sbcglobal.net> wrote in message
news:ZA**********************@newssvr28.news.prodi gy.com...
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


Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.