Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Function to pointer?

Posted on 1998-09-02
7
Medium Priority
?
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

610 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