C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 29th, 2012, 06:20 AM
joanillo joanillo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 joanillo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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");
}

Reply With Quote
  #2  
Old November 29th, 2012, 06:41 AM
mitakeet's Avatar
mitakeet mitakeet is offline
I'm Baaaaaaack!
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 5,538 mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 38 m 46 sec
Reputation Power: 242
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.
__________________

My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
Free code: http://sol-biotech.com/code/.
Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up

The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw

Reply With Quote
  #3  
Old November 29th, 2012, 06:50 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,905 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 9 m 41 sec
Reputation Power: 1774
> 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).
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Last edited by salem : November 29th, 2012 at 06:54 AM.

Reply With Quote
  #4  
Old November 29th, 2012, 06:52 AM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
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.

Reply With Quote
  #5  
Old November 29th, 2012, 10:53 AM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,938 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 43 m 16 sec
Reputation Power: 1312
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);
}

Reply With Quote
  #6  
Old November 29th, 2012, 11:45 AM
joanillo joanillo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 joanillo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 44 sec
Reputation Power: 0
Thanks, it works! (though is really difficult to understand...)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Call functions dinamically

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap