Function to pointer?

Posted on 1998-09-02
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 (...)
  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!

David Chong
Question by:Haho2
  • 3
  • 2
  • 2

Expert Comment

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.

Author Comment

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




Accepted Solution

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


#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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.


Expert Comment

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.

Expert Comment

ID: 1252485
braveheart it was just a sample.. :)

Expert Comment

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

Author Comment

ID: 1252487

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
voltage to force translation ? 8 99
C++ check and remove last word from a string 5 170
negation in C function 14 153
Trouble linking program with -lcrypt 3 149
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

776 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