Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-09-2005, 01:49 AM   #1
Registered: Mar 2004
Posts: 43

Rep: Reputation: 15
accessing functions using strings in runtime


I am designing a parser to read the input file for my code. In that, I use various commands and associate those commands with the relevant functions. Each time, i endup in adding a couple of lines like

return clear();
else if(strcmp(cmd,"copy")==0)
return copy();

All the member functions in that class are of same name as the commands. So, i need to automate this process in order to avoid the above statements. In otherwords, i want some thing like below ( function pointers? )

for(int i=0;i<num_func;i++)
return func[i]();

Please let me know.

Old 05-10-2005, 12:05 AM   #2
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
I think the only way is to create a lookup table:
itsme@dreams:~/C$ cat funcptr.c
#include <stdio.h>
#include <string.h>

void a(void)
  puts("Inside a");

void b(void)
  puts("Inside b");

int main(void)
    char *name;
    void (*func)(void);
  } list[] = {
               { "a", a },
               { "b", b },
               { NULL }
  int i;
  char input[20];

  for(;;)  // Loop forever
    printf("Enter a or b: ");

    fgets(input, sizeof(input), stdin);
    input[strlen(input) - 1] = '\0';

    for(i = 0;list[i].name;++i)  // Loop through list until NULL
      if(strcmp(list[i].name, input) == 0)

  return 0;

itsme@dreams:~/C$ ./funcptr
Enter a or b: a
Inside a
Enter a or b: b
Inside b
Enter a or b: a
Inside a
Enter a or b:
Old 05-17-2005, 06:48 PM   #3
Senior Member
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
even better yet, use a hash table.
Old 05-18-2005, 09:02 AM   #4
Registered: Nov 2004
Posts: 72

Rep: Reputation: 15
hi shyam,
cant u use a macro ??
#define CALL_FUNC(name) name()
where name is the copy, clear, or any cmd which u read from the file (provided it is valid command - function in ur case).
So u just read the word from the file and just do CALL_FUNC(name) which will call ur function. ........
M sorry u cant do in this manner... preprocessor will remove ur macro before compilation.


Last edited by mehuljv; 05-19-2005 at 06:17 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find the unrefrenced functions during the runtime ? IDoIt Programming 1 06-29-2005 08:07 AM
Accessing member functions of sockaddr_in Stack Overflow Programming 4 01-27-2005 06:11 PM
error while accessing math functions in kernel modules dypgrp Programming 0 01-19-2005 10:12 AM
accessing functions in kernel modules starbuck8968 Programming 5 12-14-2004 10:42 AM
Problem in C: arrays, functions and strings OrganicX Programming 15 03-18-2003 10:31 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:00 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration