473,383 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Difference between function pointers

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

What is the advantage of pointers to functions?
Jul 25 '08 #1
11 1468
ram kishore wrote:
What is the difference between
(int *) fun() and int *fun() ?

What is the advantage of pointers to functions?
Do your own homework

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Jul 25 '08 #2
ram kishore said:
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
Jul 25 '08 #3
zR0
On Jul 25, 12:19*pm, ram kishore <rapol...@gmail.comwrote:
What *is the difference between
(int *) fun() and int *fun() ?

What is the advantage of pointers to functions?
This can be a good resource on this topic.
http://www.newty.de/fpt/index.html
Jul 25 '08 #4
zR0 <ro***************@gmail.comwrote:
On Jul 25, 12:19=A0pm, ram kishore <rapol...@gmail.comwrote:
What =A0is the difference between
(int *) fun() and int *fun() ?

What is the advantage of pointers to functions?

This can be a good resource on this topic.
http://www.newty.de/fpt/index.html
Not if you're programming in C, it can't. That's very C++-oriented.

Richard
Jul 25 '08 #5
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
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

--
Nick Keighley

Error on line 0: Lazy programmer.
Jul 25 '08 #6
Nick Keighley <ni******************@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_Keith) ks***@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"
Jul 25 '08 #7
ram kishore wrote:
What is the advantage of pointers to functions?
All function calls are made
with an operand of pointer to function type.

--
pete
Jul 26 '08 #8
pete <pf*****@mindspring.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?

--
Keith Thompson (The_Other_Keith) ks***@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"
Jul 26 '08 #9
Keith Thompson wrote:
pete <pf*****@mindspring.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_pointer)();

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_pointer();

The other way doesn't make any sense.

--
pete
Jul 26 '08 #10
In article <Dr******************************@earthlink.com> ,
pete <pf*****@mindspring.comwrote:
>Keith Thompson wrote:
>pete <pf*****@mindspring.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_pointer)();

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_pointer();

The other way doesn't make any sense.
I may be misunderstanding 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?
Jul 26 '08 #11
co****@panix.com (Greg Comeau) writes:
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@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_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.

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)(...).

--
Ben.
Jul 27 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: rahul8143 | last post by:
hello, I require a single c++ program that will illustrate usage of this,far,near pointers. Can any body please explain me those pointers? regards, rahul.
2
by: duyifan.nju | last post by:
hello, can someone tell me what is the difference between sort() (in stl) & qsort(stdlib.h) thanks in advance.
24
by: wwj | last post by:
Hi ,all I want to know the difference between char a and char *p=new char and the difference between the heap and the stack ,and if the char a is corresponding to the stack in MEMORY,and char...
59
by: Ramkumar R K | last post by:
what is the difference between objects and pointers?
79
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
8
by: venkatesh | last post by:
hai to everybody, i am having doubt in difference between the malloc and calloc function. please tell where to use and when to use those functions?
14
by: code break | last post by:
what is the difference in this pointers decalarition ? int *ptr; and int (*ptr);
10
by: ravi | last post by:
Hi, i am a c++ programmer, now i want to learn programming in c also. so can anybody explain me the difference b/w call by reference and call by pointer (with example if possible).
6
by: mthread | last post by:
Hi, I am learning C++ and I have been told that an object can be created either by using calloc or new. If it is true, can some one tell me what is the difference b/w using these two function...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.