What is the difference between
(int *) fun() and int *fun() ?
()
What is the advantage of pointers to functions?
When you need to point to a function, nothing beats them.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Nick Keighley <nick_keighley_ nospam@hotmail. comwrites:
On 25 Jul, 08:19, ram kishore <rapol...@gmail .comwrote:
>
>What is the difference between
>(int *) fun() and int *fun() ?
>
one gives a syntax error the other doesn't
[...]
Not necessarily. I just wrote a small syntactically correct program
that uses both constructs. But one of them probably doesn't mean what
the OP thinks it means.
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
>
All function calls are made
with an operand of pointer to function type.
True.
Then the question the OP should have asked is:
What is the advantage of pointers to functions other than those that
result from the implicit conversion of a function name used as the
prefix of a function call?
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
>All function calls are made
>with an operand of pointer to function type.
>
True.
>
Then the question the OP should have asked is:
>
What is the advantage of pointers to functions other than those that
result from the implicit conversion of a function name used as the
prefix of a function call?
I recall recently,
in one of the web pages from a URL posted to this newsgroup,
advice that function calls should properly be made this way:
either
function_name() ;
or
(*function_poin ter)();
I wouldn't write function calls differently
for function name versus pointer constructs,
but if I would, I would do it this way:
either
(&function_name )();
or
function_pointe r();
In article <Drednbtjw4FtPR bVnZ2dnUVZ_uWdn Z2d@earthlink.c om>,
pete <pfiland@mindsp ring.comwrote:
>Keith Thompson wrote:
>pete <pfiland@mindsp ring.comwrites:
>>ram kishore wrote:
>>>What is the advantage of pointers to functions?
>>All function calls are made
>>with an operand of pointer to function type.
>>
>True.
>>
>Then the question the OP should have asked is:
>>
>What is the advantage of pointers to functions other than those that
>result from the implicit conversion of a function name used as the
>prefix of a function call?
>
>I recall recently,
>in one of the web pages from a URL posted to this newsgroup,
>advice that function calls should properly be made this way:
>either
function_name() ;
>or
(*function_poin ter)();
>
>I wouldn't write function calls differently
>for function name versus pointer constructs,
>but if I would, I would do it this way:
>either
(&function_name )();
>or
function_pointe r();
>
>The other way doesn't make any sense.
I may be misunderstandin g you, and/or the other P above, but seems
to me that although foo() vs (*foo)() etc is a valid discussion,
that it was not the original context of the question, which
was delving into why have functions which can be pointed to at all,
and not delving into stuff like their sytactic equivalences.
That said, as to your point, I probably agree with you and it's
kind of just another set of confusion for most people how this
can all be so:
#include <stdio.h>
void foo()
{
printf("foo\n") ;
}
void (*pf)();
int main()
{
foo();
(*foo)();
(&foo)();
pf = foo;
pf();
(*pf)();
(&pf)(); // error
}
--
Greg Comeau / 4.3.10.1 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
In article <lnfxpvpuug.fsf @nuthaus.mib.or g>,
Keith Thompson <kst-u@mib.orgwrote:
<snip>
>>A call using just the name of a declared
>>function is the common case; perhaps 99% of calls are of that form.
>>It makes sense to use the shortest form for the common case. The point
>>of writing
>>
> function_name() ;
> (*function_poin ter)();
>>
>>is of course to emphasize the fact that the latter is using a pointer
>>object, just as we distinguish between struct_name.mem ber and
>>pointer_nam e->member.
>
Yup. And there are some historical "distortion s" tossed in too
(for instance, if I recall, function_name() when function_name
was a pointer (sic) was not accepted by many early C compilers).
Absolutely. Both the old C reference manual I have and my copy of K&R
(1st edition) are quite clear that you call functions, not pointers.
In the expression form
primary-expression ( expression-list /opt/ )
the primary expression is required to have type "function returning
....". Later both say "If the name of a function appears in an
expression not in the function-name position of a call, a pointer to
the function is generated.
It took me a while to get used to the new-fangled idea of *not*
writing (*f)(...).
Comment