By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,728 Members | 2,488 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,728 IT Pros & Developers. It's quick & easy.

Pointer to a different function

P: 5
Hi every one !

I need to create a linked list of pointers to different functions.
The question is: if i have pointer lets say void (*a)(int, int) how to assigen to it addres of void (*)(void). (I need to run this functions from pointers not just store an adress).



thanks. ;)
Nov 23 '06 #1
Share this Question
Share on Google+
8 Replies


Banfa
Expert Mod 5K+
P: 8,916
If you need to run the function using the pointer then you will need a pointer to the right prototype when you try to invoke the function.

You can cast a point to function to a pointer to a differently prototyped function in the same way you can cast a pointer to float to a pointer to char.
Nov 23 '06 #2

P: 5
Could you please show me exact sintance ?
Nov 24 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
Expand|Select|Wrap|Line Numbers
  1. int mymax(int a, int b)
  2. {
  3.     return a>b?a:b;
  4. }
  5.  
  6. int main()
  7. {
  8.     void (*pfn)(void);  /* Declare function pointer */
  9.  
  10.     pfn = (void (*)(void))mymax;  /* Assign location of mymax to function pointer  */
  11.  
  12.     /* But you can use pfn to call max with casting it back to the correct type */
  13.  
  14.     return 0;
  15. }
  16.  
Nov 24 '06 #4

P: 5
Well this is your code, with small changes. But the function doesnt work from pfn pointer.

#include <iostream>

using namespace std;

int mymax(int a, int b)
{
return a>b?a:b;
}

int main()
{
void (*pfn)(void); /* Declare function pointer */

pfn = (void (*)(void))mymax; /* Assign location of mymax to function pointer */
// pfn = mymax;


pfn(1,2); // ERROR too many arguments to function


/* But you can use pfn to call max with casting it back to the correct type */

system("PAUSE");
return 0;
}
Nov 24 '06 #5

Banfa
Expert Mod 5K+
P: 8,916
It wouldn't, sorry I made a typo in my comment which should read

/* But you can't use pfn to call mymax with casting it back to the correct type */

((int (*)(int,int))pfn)(1,2);
Nov 24 '06 #6

P: 5
ok....
but I can't see how it can help me...
that what iam trying to do->

typedef void (*voidFnPtr)();

voidFnPtr p=NULL;
voidFnPtr arrfunctions[10]={NULL};
//actually i need linked list but it only example.

// Also lets say i have function
int A(int) and
char B(void)
//just 2 different functions.

then i need to do something like that =>
arrfunctions[0] = A
arrfunctions[1] = B


and be able to run it

arrfunctions[0]();
arrfunctions[1]();



can you help me?
Nov 24 '06 #7

Banfa
Expert Mod 5K+
P: 8,916
This is not possible, in order for the program to run correctly you MUST call the functions with the correct number of parameters.

You can not just store a list of pointers to void functions you have to preserve the function prototype to call them.

If you have a fixed number of function prototypes that may be in the llist then you could use a union of function pointers and an enum for each entry to indicate which is the correct one.

Just look at what you have already written

int A(int)

arrfunctions[0] = A;

arrfunctions[0]();

You would have called A that is expecting a int parameter with no parameters. This is undefined behaviour, anything might happen.
Nov 24 '06 #8

P: 5
thanks man ;)

i had a hope that there is some way around it :) .but...

well now its all clear...
Nov 24 '06 #9

Post your reply

Sign in to post your reply or Sign up for a free account.