The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Call functions dinamically
Discuss Call functions dinamically in the C Programming forum on Dev Shed. Call functions dinamically C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 29th, 2012, 06:20 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 2
Time spent in forums: 19 m 44 sec
Reputation Power: 0
|
|
|
Call functions dinamically
This code works, and the line number 13 is a comment of what I want, that is, calling a function from a string whose content is the same that the functions that I want to call.
Thanks in advance,
Joan Q
#include <stdio.h>
void execute_function( void (*function)() );
void draw_pent( );
int main(int argc, char *argv[])
{
char *func;
func = "draw_pent";
if( strcmp( func, "draw_pent" ) == 0 ) execute_function ( draw_pent );
//what I want is something like:
//execute_function ( func );
}
void execute_function( void (*function)() )
{
(*function)();
}
void draw_pent () {
printf ("draw pent\n");
}
|

November 29th, 2012, 06:41 AM
|
 |
I'm Baaaaaaack!
|
|
Join Date: Jul 2003
Location: Maryland
|
|
|
You can use function pointers if you want to dynamically change what functions are executed. It is actually fairly straightforward, though the syntax can be a bit strange. Google is your friend here.
|

November 29th, 2012, 06:50 AM
|
 |
Contributed User
|
|
|
|
> char *func;
> func = "draw_pent";
This would be
Code:
void (*func)();
func = draw_pent;
Please remember to use [code][/code] tags when posting code, so it looks like the above example (with line numbers, fixed width font and indentation).
But if you're talking about resolving a function name string at run-time, then you'll need to use something like dlopen and dlsym
Bear in mind that you don't really want the user to be able to type in any random function name and you to call it (possibly with incorrect parameters).
A simple lookup table mapping char* names to function pointers is simple, safe and effective (and portable I might add - which dlopen et al are not).
Last edited by salem : November 29th, 2012 at 06:54 AM.
|

November 29th, 2012, 06:52 AM
|
|
|
Quote: | Originally Posted by joanillo This code works |
That's the way to do it, the basic way.
You could create a hash table with the name of the function as key and the function pointer as value and use that instead of a series of strcmp().
The series of strcmp() is easier to implement from scratch.
|

November 29th, 2012, 10:53 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
If you're coding for a POSIX system (like Linux), you can also use dlfcn.h, which provides dynamic linking. This can be a convenient way to look up data (including functions) given a string. Note the pretty radical type casting, however, which is inherently dangerous. You must be absolutely sure that the function you resolve dynamically via dlsym really is a function and has the desired call signature.
Code:
/* Compile with gcc -ldl -rdynamic */
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
void testit(void) {
printf("Working!\n");
}
int main(void) {
void *handle;
void (*fn)(void);
char *error;
handle = dlopen(NULL, RTLD_LAZY);
if (!handle) {
fprintf(stderr, "%s\n", dlerror());
exit(1);
}
dlerror();
*(void **)(&fn) = dlsym(handle, "testit");
if ((error = dlerror()) != NULL) {
fprintf(stderr, "%s\n", error);
exit(1);
}
fn();
dlclose(handle);
exit(0);
}
|

November 29th, 2012, 11:45 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 2
Time spent in forums: 19 m 44 sec
Reputation Power: 0
|
|
|
Thanks, it works! (though is really difficult to understand...)
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|