LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 05-09-2005, 12:49 AM   #1
shyam_d_sundar
Member
 
Registered: Mar 2004
Posts: 43

Rep: Reputation: 15
accessing functions using strings in runtime


hi,

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

if(strcmp(cmd,"clear")==0)
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++)
if(strcmp(cmd,func_name[i])==0)
return func[i]();

Please let me know.

Regards,
Shyam
 
Old 05-09-2005, 11:05 PM   #2
itsme86
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:
Code:
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)
{
  struct
  {
    char *name;
    void (*func)(void);
  } list[] = {
               { "a", a },
               { "b", b },
               { NULL }
             };
  int i;
  char input[20];

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

    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)
        list[i].func();
  }

  return 0;
}
Code:

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, 05:48 PM   #3
jonaskoelker
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, 08:02 AM   #4
mehuljv
Member
 
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.

Bye
Mehul

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


Reply



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 07:07 AM
Accessing member functions of sockaddr_in Stack Overflow Programming 4 01-27-2005 05:11 PM
error while accessing math functions in kernel modules dypgrp Programming 0 01-19-2005 09:12 AM
accessing functions in kernel modules starbuck8968 Programming 5 12-14-2004 09:42 AM
Problem in C: arrays, functions and strings OrganicX Programming 15 03-18-2003 09:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:38 PM.

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