Archived

This topic is now archived and is closed to further replies.

blide

Pointers to functions

Recommended Posts

blide    122
Hi all, I have heard somethings about pointers to functions and I am wondering what that is and how it works. Could someone please tell how to make a pointer to a function and why it would ever be useful, thanks! My guess is it would look like making a normal pointer but I have no idea and I still don''t know how to use it. My idea:
    
void func()
{
}
void* fp;
fp = &func;
//and to use it

(*fp)();
    
Another thing is i have no idea how a function is stored...is it like a variable?...Thanks for any input! -blide blide@mail.com

Share this post


Link to post
Share on other sites
DDnewbie    122
Hi,

I guess it''s an alternative to virtual functions. I think pointers to functions are faster than virtual functions, (correct me if I''m wrong).

That is ONE usage of them!


/Mankind gave birth to God.

Share this post


Link to post
Share on other sites
Aldacron    4544
blide -

Thinking in terms of C and not C++, funtion pointers have severl uses. A common use is as a callback function. A good example of this is the WindowProcedure you have to write to handle window messages. Or, say your writing a multi-purpose graphics library. In order to handle different color depths you could have a function pointer for each routine that would be different at each depth. For example, Put_Pixel pointer and the functions Put_Pixel15, Put_Pixel16, etc...

As to declaring and using functions, your syntax is a bit off.

    
// declaration

void (*fp1)(void);
int (*fp2)(char *arg);

// functions

void func1 (void)
{
}

int func2 (char *arg)
{
return 0;
}

// assignment

fp1 = func1;
fp2 = func2;

// usage


// K&R style

(*fp1)();
(*fp2)("Hello");

// ANSI

fp1 ();
fp2 ("Hello");


Both styles of calling functions through pointers are acceptable in ANSI C, but K & R C only uses the first. Not a big deal these days.

DDNewbie -

In C++, pointers *are* an alternative to virtual functions, and in most case virtual functions suit you better if you are working with classes. For example, using the graphics lib example above, you could define a pure virtual base class called CGraphics, and inherit from that with CGraphics15, CGraphics16, etc...

Share this post


Link to post
Share on other sites
Neuro    123
Another use is when wanting to pass a pointer to a function in as part of the parameter list for another function.

A good example of this is in the qsort function in C. You pass in a pointer to the compare function.

-----------------------------------------------
All messages are of my own personal opinion and
not meant to offend. But if they do - tough :)

Neuro.

Share this post


Link to post
Share on other sites
ImmaGNUman    122
Pointers to functions can be used in various ways. By far, one of the most powerful being an array of pointers to functions.

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."

Share this post


Link to post
Share on other sites
blide    122
Thanks a lot for all the help guys...I understand how pointers to functions work now...and I guess I see some uses for them although not that many. Thanks again.

-blide
blide@mail.com

Share this post


Link to post
Share on other sites