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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.


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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 and use structures in the C programming language.
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.

839 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