LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 07-14-2011, 08:49 PM   #1
kvm1983
Member
 
Registered: Jul 2009
Posts: 47

Rep: Reputation: 1
Function pointers with variable arguments


I need to call functions that match an input string (if input str = "func1", call func1), so I have this:

Code:
#include <stdio.h>
#include <string.h>

void function_a(void) { printf("Function A\n"); }
void function_b(void) { printf("Function B\n"); }
void function_c(void) { printf("Function C\n"); }
void function_d(void) { printf("Function D\n"); }
void function_e(void) { printf("Function E\n"); }

const static struct {
    const char *name;
    void (*func)(void);
} function_map [] = { 
    { "function_a", function_a },
    { "function_b", function_b },
    { "function_c", function_c },
    { "function_d", function_d },
    { "function_e", function_e },
};

int call_function(const char *name)
{
    int i;

    for (i = 0; i < (sizeof(function_map) / sizeof(function_map[0])); i++) {
        if (!strcmp(function_map[i].name, name) && function_map[i].func) {
            function_map[i].func();
            return 0;
        }   
    }   

    return -1; 
}

int main()
{
    call_function("function_a");
    call_function("function_c");
    call_function("function_e");
}
In the above example, the functions take no input arguments. Can they take a different number of arguments, for example, function_a(int), function_c(int, int), function_e(int, char, int)? How can I do that?

Thanks,
Kshitij
 
Old 07-14-2011, 09:05 PM   #2
kvm1983
Member
 
Registered: Jul 2009
Posts: 47

Original Poster
Rep: Reputation: 1
I think I HAVE to do something like this:

if( strcmp(funcname, "function_a") == 0)
function_a(int);
else if ( strcmp(funcname, "function_b") == 0)
function_b(int, int);

I was looking for a more graceful way of doing it, but I dont think there is. Maybe I asked the question prematurely.
 
Old 07-14-2011, 09:24 PM   #3
flamelord
Member
 
Registered: Jun 2011
Distribution: Arch Linux
Posts: 151

Rep: Reputation: 34
Yes, it is possible. If I understand what you are asking, then you would have something like:

Code:
#include <stdargs.h>

/*
...
*/

int call_function(const char *name, ...)
{
    int i;
    va_list argp;

    va_start(argp, name);

    for (i = 0; i < (sizeof(function_map) / sizeof(function_map[0])); i++) {
        if (!strcmp(function_map[i].name, name) && function_map[i].func) {
            function_map[i].func(argp);
            va_end();
            return 0;
        }   
    } 
    
    va_end();  

    return -1; 
}
And then your functions will need to be of the form
Code:
void function_x(va_list args);
And within the function you will need to extract the arguments using va_arg(args,<type>).

What I have given you is only a rough outline, but it gives you the general idea. You may also want to look at http://c-faq.com/~scs/cclass/int/sx11b.html
 
1 members found this post helpful.
Old 07-14-2011, 10:12 PM   #4
kvm1983
Member
 
Registered: Jul 2009
Posts: 47

Original Poster
Rep: Reputation: 1
Thank you very much. That helped.
 
  


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
function showing a list of variable and value: (dynamic variable name) budhax Linux - Newbie 1 09-19-2008 07:05 PM
vimrc function !./% with variable for arguments sadarax Linux - Software 0 06-22-2007 03:13 PM
Question about a function that takes a variable amount of arguments daYz Programming 5 03-15-2007 01:59 AM
Pointers/Passing arguments to functions question (C) smoothdogg00 Programming 7 03-17-2006 01:46 PM
Linked Lists: Pointers sent to functions as arguments do not change their valur Jose Muņiz Programming 3 01-12-2004 07:45 PM

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

All times are GMT -5. The time now is 02:25 AM.

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