Solved

Function to pointer?

Posted on 1998-09-02
7
275 Views
Last Modified: 2010-05-18
Hi, I am trying to improve my knowledge of C.
I would like to know what is the difference between
a function declared as :
  1)  eg.   void MyFunction (...)
and
  2)  eg.   void *Myfunction (...)
I know 2) is a pointer to a funciton but when would I want to use 1) or 2) and why??? a simple example would be great!
Thanks!!!!

David Chong
0
Comment
Question by:Haho2
  • 3
  • 2
  • 2
7 Comments
 
LVL 3

Expert Comment

by:braveheart
ID: 1252481
I am sure you know when you would use 1) but a pointer to a function is useful where you wish to call different functions according to the data that you wish to process.

For instance, you may wish to use different functions to draw the outline of a shape or to draw a filled shape, according to some shading mode that has been set earlier in the program.  Of course you could always test which mode you are in before calling the function but you would have to do this wherever it is called.  

You could move the test inside some wrapper function or macro but a more elegant solution would be to set the value of the function pointer appropriately when the mode is first set and then invoke the drawing function via the pointer when it is wanted. Such a  function is often known as a callback.

Of course, you might want to add more modes, so you would have to extend the test to a switch statement if you were not using the callback approach.

Even more fundamentally, if you are writing a library for use by someone else, you cannot foresee all the possible ways in which the library may be used and you want to make it easy to extend. This is achieved more easily by allowing users to define their own callbacks than by trying to predict what they may want to do.
0
 

Author Comment

by:Haho2
ID: 1252482
Thanks,
   but I have seen sample code from C books that uses 2) but it is not used for a callback function purposes..it is just a usual function with a pointer to it... is there any other reasons???

Bye!

David

0
 
LVL 3

Accepted Solution

by:
xyu earned 50 total points
ID: 1252483
1) void Function(); it is a prototype of the function that returns nothing...

2) void *Function(); is a prototype of the function that returns pointer to something...

if You want to declare the pointer to the function do it this way:
void (*Function)();

call to it is (*Function)() or even Function();

or event better this way

typedef void (*TMyFunctionPtr)();
TMyFunctionPtr p_fnMyFunction;

call is (*p_fnFunction)() or p_fnFunction();

Good luck

example:

#include <stdio.h>

typedef void (*TFuncToCallPtr)();

void Test0() {printf("Test0\n");}
void Test1() {printf("Test1\n");}
void Test2() {printf("Test2\n");}
void Test3() {printf("Test3\n");}

TFuncToCallPtr NumToFunc(int i) {
      switch (i) {
            case 0: return &Test0;
            case 1: return &Test1;
            case 2: return &Test2;
            default: return &Test3;
      } /*switch (i)*/
} /*NumToFunc(int)*/

void main()
{
      for (int i = 0; i < 5; ++i) {
            TFuncToCallPtr p_fnFunc = NumToFunc(i);
            if (p_fnFunc) (*p_fnFunc)();
      } /*for (int i)*/
} /*main()*/


Good luck
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

by:braveheart
ID: 1252484
Parsers often make use of the "pointer to function" mechanism so that switch statements don't grow too big. For instance, to keep track of the pointers you only need an array or two, rather than hardcoding a switch statement.
0
 
LVL 3

Expert Comment

by:xyu
ID: 1252485
braveheart it was just a sample.. :)
0
 
LVL 3

Expert Comment

by:braveheart
ID: 1252486
xyu, my remark was not a comment on your example but a response to Haho2's subsequent question "is there any other reasons???" (sic).
0
 

Author Comment

by:Haho2
ID: 1252487
thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now