1

Possible Duplicate:
Pass arguments into C program from command line

I am trying to pass three arguments from terminal into a function called replace. I would like to know if it is possible to do the following from terminal

  % ./replace d DDD mytest.tx

I have looked online but can only find information on passing values directly to main() and not the function inside.

Edit: I have edited the main functions as following:

void replace(char* string_a, char* string_b, char* string_f)
 {
  }

int main(int argc, char *argv[])
  { 
       if(argc < 4)
  { 
    printf("Not enough arguments\n");
   return 0;
  }

replace(argv[1],argv[2],argv[3]);
 }
Community
  • 1
  • 1
user1072706
  • 573
  • 2
  • 8
  • 20
  • 2
    Why not pass them to `main()` then to the function from there? Why are you trying to by-pass sending them to `main()`? – Mike Sep 25 '12 at 14:44

6 Answers6

5

Your main function should be in the format:

int main ( int argc, char *argv[] )

argv is a pointer to your arguments. Note that the first argument is the name of your program.

Here is a lesson on command line arguments:

Lews Therin
  • 10,907
  • 4
  • 48
  • 72
2

Course you can, main prototype is int main(int ac, char **av) where ac is the number of arguments passed to the program and char** is an array of arrays containing the arguments passed to the program.

For example for your code :

int main(int ac, char **av)
     {
      void replace (av[1], av[2], av[3])
        { 
         .......
        }

     }

If you launch your exec as : ./replace d DDD mytest.tx, av[0] will be your program name, av[1] will be d, av[2] DDD and av[3] mytest.tx

Good luck !

Simon MILHAU
  • 214
  • 1
  • 8
1

All stand-alone proper C programs start their execution in main(). That's just how the language works.

So what you need to do is call replace() from main(), after verifying and interpreting the arguments. The command-line arguments will be in the standard int argc, char *argv[] parameters to main().

unwind
  • 391,730
  • 64
  • 469
  • 606
1

The thing you want to do is use main's argument list.

You can use the following signature:

int main(int argc, char* argv[]), where argv is a pointer to the argument list, passed from the command line.

1

Look up argc and argv.

argc is the number of arguments and argv is an array of pointers to your arguments.

so your code should look something like this:

void replace (char string_a[],char string_b[], char string_f[])
{
    //...
}

int main(int argc, char *argv[])
{
    if(argc < 4)
    {
        printf("Not enough arguments\n");
        return 0;
    }

    replace(argv[1], argv[2], argv[3]);
}

Also remember that the first item in argv (argv[0]) is the path of the executing program.

123
  • 5,666
  • 7
  • 25
  • 30
1

To answer your exact question: I am trying to pass three arguments from terminal into a function called replace. No, you cannot by-pass sending the arguments to main.

The C language is defined such that it looks for a main() entry point into the program, it complains if you don't have one. Because main() is expected to be present it is used as the entry point for any command line arguments:

[void | int] main(int argc, char *argv[]) 

where argc is the number of command line arguments and argv[] is an array of character pointers ("string" versions of your argument list)

If you want to pass them to replace you really just have to send them straight through:

int main(int argc, char *argv[]){
  if argc is big enough to satisfy
     replace(argv's)

If you really don't want main() knowing about the arguments you need to store they in a file or get them at run time from stdin from your replace() function.

If you're trying to have "replace" BE your main function, that can be possible if your linker allows you to define the entry point.

Again... not really sure what your goal is here.

Mike
  • 47,263
  • 29
  • 113
  • 177
  • Thanks for the explanation. No, I am not trying to bypass main; my goal is simply to call replace from terminal. – user1072706 Sep 25 '12 at 15:15