473,405 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Pointer to a different function

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
8 1821
Banfa
9,065 Expert Mod 8TB
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
Could you please show me exact sintance ?
Nov 24 '06 #3
Banfa
9,065 Expert Mod 8TB
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
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
9,065 Expert Mod 8TB
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
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
9,065 Expert Mod 8TB
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
thanks man ;)

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

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

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

Similar topics

3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
11
by: Rajesh | last post by:
Dear All, Please let me know the advantage of function pointer? Is it fast calling function using function pointer? Is it possible to use function pointer to optimise code? Thanks and regards...
52
by: Douglas Garstang | last post by:
I can't believe I've been trying to work this out for hours now, and I can't believe I couldn't find someone asking for a similar solution in the newsgroups. No wonder I hate C so much, and every...
35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
5
by: Danilo Kempf | last post by:
Folks, maybe one of you could be of help with this question: I've got a relatively portable application which I'm extending with a plugin interface. While portability (from a C perspective) is...
5
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read...
49
by: elmar | last post by:
Hi Clers, If I look at my ~200000 lines of C code programmed over the past 15 years, there is one annoying thing in this smart language, which somehow reduces the 'beauty' of the source code...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
8
by: tfelb | last post by:
Hey group! I have 2 questions. I saw functions with char *dst = (char *)src. In that case if I remember what I've learned I assign (an) (the) address of src to dst. Right? But I can assign...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.