Difference between function pointers

R

ram kishore

What is the difference between
(int *) fun() and int *fun() ?

What is the advantage of pointers to functions?
 
N

Nick Keighley

What  is the difference between
(int *) fun() and int *fun() ?

one gives a syntax error the other doesn't
What is the advantage of pointers to functions?

1. call backs
2. threaded interpreters
3. really cool looking code

look up qsort() in your textbook
 
K

Keith Thompson

Nick Keighley said:
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.
 
K

Keith Thompson

pete said:
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?
 
K

Keith Thompson

The original question,
as well the web page advice that I disagreed with,
both seemed not to acknowledge
that a function call is an operation which takes
an expression of a function to pointer type as an operand

There are a lot of fundamental concepts
such as "what is a function call?",
that you really don't need to have a complete understanding of
in order to write a lot of correct C programs,
but I like to expound upon them when I can anyway.

You didn't exactly expound on the point. You mentioned it without
explaining it, which I suspect wasn't of much help to the OP.

As for using a different syntax for calls using a function name
vs. calls using a function pointer, I don't think that

(&function_name)();

makes a whole lot of sense. 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_pointer)();

is of course to emphasize the fact that the latter is using a pointer
object, just as we distinguish between struct_name.member and
pointer_name->member.

I actually haven't written enough indirect function calls like this to
decide which style I like best. Adding the unnecessary unary "*"
operator in a sense works against the rules of the language, which is
the same reason I dislike "char *argv[]" and prefer "char **argv".
But I can certainly see the benefit of distinguishing between direct
and indirect function calls. And an ordinary function call such as
"function_name()", though it does involve a pointer function according
to the rules of the language, most likely *doesn't* do so either in
the generated code or in the minds of most programmers.
 
B

Ben Bacarisse

Keith Thompson <[email protected]> wrote:

Yup. And there are some historical "distortions" 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)(...).
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top