Thread: Function execution question

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    66

    Function execution question

    Hi.

    I`m working on an application where i receive a function name and some parameters from a socket, and i have to execute the corresponding function. For example if i receive the string "hello_world" then i have to execute the function hello_world() located let`s say in service.c.

    I`ve red the documentation of the dlopen function, but it isn`t good for me, because i compile the service.c file with my application, it is not an external object file.

    Every suggestion is welcome.

    Thanks

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yep... find a better way of doing this.

    C is not going to let you execute a function by naming it in a string...

    You can however use that string as a "menu selection" and call the function indirectly...

    Code:
    if (!strcmp(str,"Hello_World"))
      hello_world();

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    66
    Yes, i thought about this solution i just wanted to know if exists any other solution to automate this part, because the other way if i invent a new function every time i have to come back, and add it manually. Not that this would be a big problem...

    Thanks

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by raczzoli
    Yes, i thought about this solution i just wanted to know if exists any other solution to automate this part, because the other way if i invent a new function every time i have to come back, and add it manually. Not that this would be a big problem...
    You can map strings to function pointers such that you can search for the string, obtain the function pointer, then call the function. This way, you just need to modify the contents of the data structure instead of adding another branch to an if-else chain.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by laserlight View Post
    You can map strings to function pointers such that you can search for the string, obtain the function pointer, then call the function. This way, you just need to modify the contents of the data structure instead of adding another branch to an if-else chain.
    A very basic example of this would be:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void  myHello(void);
    void  myGoodBye(void);
    
    int main(void){
    
    	//create framework for function table
    	struct myFunctionTable{
    		char *myName;
    		void (*myFunction)(void);
    	};
    
    	//make function table, aka array
    	myFunctionTable FunctionTable[]={{"Hello", &myHello}, {"GoodBye", &myGoodBye}};
    
    	char myAnswer[10];
    
    
    	printf("Which function to run?");
    	scanf("%s", myAnswer);
    
    	//find and run function
    	for(int i = 0; i < (sizeof(FunctionTable) / sizeof(myFunctionTable));i++){
    		if(strcmp(FunctionTable[i].myName,myAnswer)==0){
    			FunctionTable[i].myFunction ();
    			break;
    		}
    	}
    	getchar();
    	getchar();
    	return (0);
    }
    //Implement functions
    void myHello(void){
    	printf("Hello World\n");
    }
    void myGoodBye(void){
    	printf("GoodBye World\n");
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. measuring function execution time
    By nik in forum C++ Programming
    Replies: 5
    Last Post: 03-12-2011, 09:21 PM
  2. calcualte the execution time of a function
    By George2 in forum C Programming
    Replies: 2
    Last Post: 06-15-2006, 01:27 AM
  3. Scheduling a program/function execution?
    By Aedile in forum Linux Programming
    Replies: 1
    Last Post: 08-12-2005, 10:19 AM
  4. dynamic function execution
    By Sargnagel in forum C Programming
    Replies: 7
    Last Post: 05-07-2003, 05:28 AM
  5. randomizing order of execution of function
    By y2jasontario in forum C Programming
    Replies: 2
    Last Post: 04-03-2002, 07:50 PM