Call a funvtion from a variable

This is a discussion on Call a funvtion from a variable within the C Programming forums, part of the General Programming Boards category; Hi there. I am trying to implement a very basic shell for a flat file system. Anyhow, my shell will ...

  1. #1
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68

    Call a funvtion from a variable

    Hi there. I am trying to implement a very basic shell for a flat file system. Anyhow, my shell will be very simple for now. It will be an endless while loop where I prompt for input and after reading it in a string with fgets I use sscanf to break it up ( I suppose I could use a delimiter as well but since all comands are separated by spaces I guess it's okay)
    and then I was thinking of how to call the appropriate function.
    I thought of comparing the command with all available commands or using table with pointer (well actually I havent thought much about that yet, just that it came to me that maybe I could use funtion pointers) but I wanted to ask here first (I searched a little but couldnt find anything helping me as far aas c is concerned)
    Is there any way to use a string variable to call a function

    Here is what my code would like:

    Code:
    while(1) {
    printf("prompt> ");
    ...
    if(!fgets(line,sizeof(line),stdin)) break;
    ...
    args = sscanf(line,"%s %s %s",cmd,arg1,arg2);
    ...
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You're on the right track with a table with strings and fp.

    Ie,
    Code:
    typedef struct tableRow
    {
       char name[64];
       void (*fn)(int, char **);   /* You can do the same thing as main(int argc, char * arv[]) now */
    } tableRow_s;
    Last edited by zacs7; 01-18-2009 at 06:29 AM.

  3. #3
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    Yeah, I know I am a bother but could someone explain the above a little to me. As I said it was just a hunch but I really don't understand it fully yet. Or some link with explanation and/or examples
    Am I to creata a table with strings (the names of the functions) and next to them have a pointer to that function. So I search and compare the table and if succesfull call the pointer next to it? But then what type of pointer will I put. Some gunctions take no arguments while other take various. Dont I need to make a pointer to the exact type of function
    Last edited by Phoenix_Rebirth; 01-18-2009 at 06:49 AM.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Again you have the idea, have a function pointer like main and pass argc (the amount of aruments) and argv (a vector of arguments). If no arguments, pass 0 for argc and NULL for argv.

  5. #5
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Location
    Cyprus
    Posts
    68
    Oh... Okay, Yes I see. (feeling a little dumb) Thanks zacs7

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. minix system call pls help for project
    By porvas in forum Linux Programming
    Replies: 2
    Last Post: 06-14-2009, 03:40 AM
  2. How to get RSSI value, send to sensor, sensor receive package, repackage it?
    By techissue2008 in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-04-2009, 10:13 AM
  3. Problem with a char variable set as a letter
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2004, 01:25 PM
  4. C system call and library call
    By Coconut in forum C Programming
    Replies: 6
    Last Post: 08-22-2002, 12:20 PM
  5. Assembly example
    By Lynux-Penguin in forum C Programming
    Replies: 6
    Last Post: 04-24-2002, 08:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21