473,326 Members | 2,136 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,326 software developers and data experts.

application of pointers to functions

sid
Hi!
can anyone please tell me the use (application) of pointers to
functions?
e.g. void *func()
{
_ _ __
_____
}

Jun 27 '07 #1
2 1354
On Wed, 27 Jun 2007 10:37:18 -0000, sid <ki***********@gmail.com>
wrote:
>Hi!
can anyone please tell me the use (application) of pointers to
functions?
e.g. void *func()
{
_ _ __
_____
}
Bad example. This is not a pointer to function. It is a function
returning pointer to void.

For a specific example, look in your reference at the qsort function
which takes a pointer to function as one of its parameters.

In short, you use a pointer to function whenever you want to refer to
a function indirectly.

One reason is because the determination of which function to
call is made at run time as in:
if (...) func_ptr = func1;
else if (...) func_ptr = func2;
...
func_ptr(...);

Another reason is because a pointer to function can appear in
certain places where a function cannot, such as in the argument and
parameter lists of a function (e.g., qsort again) or as a member of a
struct as in
struct x {
int x1;
float x2;
void (*func_ptr)(int,float);};

There are probably other reasons which I haven't thought of at
the moment.

As with all "when do I use a particular language feature" questions,
the answer is when that feature of the language helps you perform the
task you are designing the software for.

Language tutorials tell you how to use a language feature but your
question is one of design.
Remove del for email
Jun 27 '07 #2
Chris Dollin wrote:
sid wrote:
> can anyone please tell me the use (application) of pointers to
functions?

I'm tempted to just point out that you didn't even bother to
google "function pointer" before posting your message. But
today I'm feeling generous.

When you want to be able to supply different functions to the same
code at different times. You can usefully think of them as function
variables.

An example right there in the C library is qsort, which takes a
pointer-to-function which tells it how to compare the items being
sorted. Since qsort sorts arrays of pretty much anything, clearly
it can't know by itself how to do the comparison.
Or for specifying "call-backs", like the use of the "atexit()"
function which specifies a function to be called when the program
calls "exit()".

Function pointers can also be returned from another function.

Plus you are allowed to find your own uses for function pointers. ;-)

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
Jun 29 '07 #3

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

Similar topics

1
by: Alex | last post by:
Is there any problem with sending function pointers through in a variable argument list? I have a function like the following: typedef (*ptr2FuncType1)( int ); typedef (*ptr2FuncType2)( double...
2
by: Thomas Matthews | last post by:
Hi, I would like to create a table (or vector) of pointers to templated functions. 1. How do I declare a typedef of a pointer to a templated function? For example, I have some functions...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
13
by: munni | last post by:
hi i want to write a program on function pointers where i have not written any programs using function pointers till now. i want to take an array of 26 functions which r to be pointed by this...
15
by: Christian Christmann | last post by:
Hi, in which situations pointers to functions might be more efficient/convenient than a direct function call? In the example I've found so far, I see no advantage of using pointers to...
2
by: davidzada | last post by:
Hi guys ! I try to work with application's pointers in c++ , and .. I have some problems ! I have base class that contain definition of pointer to application. This application's pointer...
1
by: Bushido Hacks | last post by:
A private utility function using a function pointer sounds ideal to me. I want to simpify writing the same set of loops. While there are a few functions that can't use it, setting and modifying...
12
by: bgold | last post by:
Hey. I have a base class (SPRITE), and using this base class I have derived a large number of derived classes (PERSON, BULLET, MISSILE, etc.). Now, at a certain point in my program, I have a pair...
2
by: =?Utf-8?B?SmFuIExpbmRo?= | last post by:
Hi Is there a preferable way to write a C# dll that should be called from an existing application (running native code). It is not possible to change this application and its source code isn't...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.