Home > OS >  How to call another function that utilizes argc, char *argv[] in main without passing things to it?
How to call another function that utilizes argc, char *argv[] in main without passing things to it?

Time:11-10

say there is a function in my code: int getwords(int argc, char *argv[]) and I want to call this function in main(). How do I call this in main without erroring out?

`void main(void)
    getwords();`

CodePudding user response:

If you write main to provide access to argc and argv:

int main(int argv, char **argv) {
  // ...
}

Then you can pass those to another function when you call it:

void foo(int argc, char **argv) {
  // ...
}

int main(int argc, char **argv) {
  foo(argc, argv);
}

argc will be copied, but argv is a pointer, so the pointer is copied but not the data it points to.

CodePudding user response:

if for some special reason you do not want to use parameters you can use global variables available in all program.

int gargc;
char **gargv;

int main(int argc, char *argv[])
{
    gargc = argc;
    gargv = argv;

    getopt();
}
  •  Tags:  
  • c
  • Related