Improve company productivity with a Business Account.Sign Up


Function to pointer?

Posted on 1998-09-02
Medium Priority
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 150 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
Become a Leader in Data Analytics

Gain the power to turn raw data into better business decisions and outcomes in your industry. Transform your career future by earning your MS in Data Analytics. WGU’s MSDA program curriculum features IT certifications from Oracle and SAS.  


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

Get Cisco Certified in IT Security

There’s a high demand for IT security experts and network administrators who can safeguard the data that individuals, corporations, and governments rely on every day. Pursue your B.S. in Network Operations and Security and gain the credentials you need for this high-growth field.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops 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.

585 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