0

I am trying to pass multiple parameters when running a compiled C code

code would be like this

void main(char argc,char *argv[]){

    printf("%s",argv[1])    //filename
    FILE *file = fopen(argv[1], "r")

    printf("%s",argv[2])    //function to be called
    char* func_name = argv[2];

    printf("%s",argv[3])    //how many times the function is called
    int repeat = argv[3];

    for(int i=0;i<repeat;i++){
        func_name(file) //calls some function and passes the file to it 
    }

}

i would compile like this

gcc cprog.c -o cprog

run like -

./cprog textfile.txt function1 4 

how do i do this ? any help would be appreciated !

9
  • You do it like that (that is, if your question is about command line parameters, if your question is about calling a function from its name, that's another question). Commented Aug 13, 2013 at 11:28
  • Remember that the entries in the argv array are strings and you can't simply can't cast a string into a number, you have to use e.g. strtol to convert the string. Other than that your code is fine with regards to the command line arguments. Commented Aug 13, 2013 at 11:28
  • You won't be able to pass the name of the function to be called via argv[2]. Commented Aug 13, 2013 at 11:28
  • I am afraid it won't be easy, as C does not store function names in the binary. You'll have to make some kind of calling table where you link strings to function pointers. Commented Aug 13, 2013 at 11:30
  • @bart ...ok, then I could have some thing like a number from command line and then a if-else block to call particular functions perhaps ? Commented Aug 13, 2013 at 11:32

3 Answers 3

1

First off:

  1. You are missing some semicolons, so your code won't even compile.
  2. argv[] are strings, so you'll have to convert them to integers if you want to use them as such.
  3. C does not store function names in the binary, so you have to create some kind of calling table.

Below find a working example. I creates a struct that maps a name to a function, implement that function and go look for it. It's quite buggy (no input validation is done), but gives you a proof of concept on how to possibly implement this.

#include <stdlib.h>
#include <stdio.h>

struct fcn_entry {
  char *name;
  void (*fcn)(char *);
};

void fcn1(char *fn) {
   printf("fcn1: %s\n", fn);
}

void fcn2(char *fn) {
   printf("fcn2: %s\n", fn);
}

void main(char argc,char *argv[]){
    // name-to-function table
    struct fcn_entry entries[] = {
        { "fcn1", fcn1 },
        { "fcn2", fcn2 },
        { NULL, NULL }
    };
    void (*fcn_to_call)(char *);
    int i = 0;

    printf("%s",argv[1]);    //filename

    printf("%s",argv[2]);    //function to be called    
    char* func_name = argv[2];
    i = 0;
    while(entries[i].name != NULL) {
        if (strcmp(entries[i].name, func_name) == 0) {
           fcn_to_call = entries[i].fcn;
           break;
        } else {
           fcn_to_call = NULL;
        }
        i++;
    }


    printf("%s",argv[3]);    //how many times the function is called
    int repeat = atoi(argv[3]);

    for(i=0;i<repeat;i++){
        fcn_to_call(argv[1]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

There are a lots of error here.

int repeat = argv[3]; //You must convert char* to int before assignment.
func_name(file)       //func_name is a char* not a function. C does not support reflection so there is no way to call function like this.

Comments

1

To be able to call a function that you have as a string, you have know which name is paired to which function.

If all functions take the same arguments, you can have an array of structures with name and function pointer, and then match the name with the correct entry in the table.

Otherwise, if the arguments are different you have to have a chain of strcmp calls to call the correct function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.