You are not logged in.
I need to write a function that is given a function pointer, and a list of types (integers that represent a type). I can obtain the values to pass to the function pointer, but am wondering how to call it. This needs to happen at run time and i dont know how many arguments i will have. e.g.
void call_function(void (*func)(),int types[],void *args[]){
int i;
for(i=0;types[i]!=0;i++){
// Add this argument
}
// call the function with the arguments
}
HP DV6 + Arch + Openbox
Offline
Use va_args and make a variadic function.
Common examples can be seen in error functions that wrap a printf.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Online
the function pointer doesn't take a function, and I don't think there is a way the create them. If there is a way to create them, how do I pass it to a function that doesn't take one?
HP DV6 + Arch + Openbox
Offline
Huh? Pronoun overflow error.
Why would the function pointer need to take a function? What them are you trying to create? What them became an it when being passed to what?
Last edited by Trilby (2013-03-22 05:14:53)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Online
Why would you do that? I can only think it could be useful in interpretet languages. You might want to look into dyncall, there are others but I don't remember the names.
| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |
Offline
Can you give more details of what you need to do? Are you trying to figure out the function, or how to put the variables together and call the function?
It doesn't make much sense to pass a function pointer in C. Are you sure that's a requirement?
This smells of a homework assignment, so I'll help, but I'm going to be just vague enough so you still have to figure out yourself. buahahaha, I just need a more specific description of what needs to be done.
I have wasted atleast a second of your time by making you read my signature.
Offline
It doesn't make much sense to pass a function pointer in C. Are you sure that's a requirement?
Sure it does. How else would you implement a callback function ??
Very useful in GUIs, I/O processing, error handling,
edit: For example, look at this page talking about Gtk. Look at the g_signal_connect function.
http://zetcode.com/tutorials/gtktutorial/gtkevents/
Last edited by ewaller (2013-03-22 05:14:58)
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
Why would you do that? I can only think it could be useful in interpretet languages. You might want to look into dyncall, there are others but I don't remember the names.
Yeh its an interpreted language, dyncall looks like what i need. Ill look into it furthur
Why would the function pointer need to take a function?
My bad thats meant to be va_list
This smells of a homework assignment
nah mostly for fun but it may be usefull down the line in other projects
Last edited by BennyBolton (2013-03-22 05:18:50)
HP DV6 + Arch + Openbox
Offline
I just found passing a function pointer was the best way to do something interesting with my WM.
I had a function that selected and called a window tiling function.
But when I started adding multimonitor support, I needed a function that did a few housekeeping bits, then tiled each monitor. It was ugly and repetative until I had the first function pass a pointer to the tiling funtion to the monitor function. The monitor function loops through monitors and calls whichever function it was passed.
But back OT. I may have misread the first time. Are you writing just the calling function, or are you also writing the function that is being passed as a function pointer? If you are only doing the former, you cannot do this. You must call a function with the correct number of arguments - unless it was declared as a variadic function. If you are writing that function to - then see va_args, and you can do this.
In either case - this may be a good place for some inline assembly. May being the operative word there - I do a good bit of C, and have dabbled in assembly, but I've never really combined the two. But as I understand it, you should be able to push your arguments to the stack, then call the function. This is how arguments are passed afterall.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Online
But back OT. I may have misread the first time. Are you writing just the calling function, or are you also writing the function that is being passed as a function pointer? If you are only doing the former, you cannot do this. You must call a function with the correct number of arguments - unless it was declared as a variadic function. If you are writing that function to - then see va_args, and you can do this.
I was probably not clear in the first post. I'm writing an interpreted language, so in c someone can register a function. This function can take any amount of arguments. I know the values to pass for the arguments, but not how. It looks like dyncall as suggested by progandy can do this for me
HP DV6 + Arch + Openbox
Offline
Trilby,
I am not sure I agree with you. Look at this code sample that is in the wikipedia page I linked in my last post:
void (*callback)(void *);
int main(void)
{
MyMsg msg1;
msg1.appId = 100;
strcpy(msg1.msgbody, "This is a test\n");
/*
* Assign the address of the function 'myfunc' to the function
* pointer 'callback'
*/
callback = (void *)myfunc;
/*
* Call the function
*/
callback((MyMsg*)&msg1);
return 0;
}
Look how callback is defined
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
@Benny, Cool, I had never heard of dyncall, but I just read a (very) little on it - it seems to do basically what my assembly suggestion was getting at - but it does the dirty work for you. I'll have to remember that one.
ewaller, was that to disagree with my claim that the number of parameters must be correct? That callback example, and the call, both have one parameter. It is typecast from a void * to a different pointer - but a pointer is a pointer, and types are often easily cast to other types. It's still the same number of parameters (i.e., one).
Last edited by Trilby (2013-03-22 05:36:20)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Online
Trilby,
I am not sure I agree with you. Look at this code sample that is in the wikipedia page I linked in my last post:void (*callback)(void *); int main(void) { MyMsg msg1; msg1.appId = 100; strcpy(msg1.msgbody, "This is a test\n"); /* * Assign the address of the function 'myfunc' to the function * pointer 'callback' */ callback = (void *)myfunc; /* * Call the function */ callback((MyMsg*)&msg1); return 0; }
Look how callback is defined
I'm confused. Shouldn't it be callback = &myfunc?
That would give it the address location of myfinc, if I'm not mistaken...
Last edited by MagicSkyFairy (2013-03-22 06:16:38)
I have wasted atleast a second of your time by making you read my signature.
Offline
The `&` is optional for functions. A "function" is already an address, there is no other meaning.
Offline