I have a program that can accept command-line arguments and I want to access the arguments, entered by the user, from a function. How can I pass the *argv[], from int main( int argc, char *argv[]) to that function ? I’m kind of new to the concept of pointers and *argv[] looks a bit too complex for me to work this out on my own.
The idea is to leave my main as clean as possible by moving all the work, that I want to do with the arguments, to a library file. I already know what I have to do with those arguments when I manage to get hold of them outside the main. I just don’t know how to get them there.
I am using GCC.
Thanks in advance.
Just write a function such as
and call that in
mainasparse_cmdline(argc, argv). No magic involved.In fact, you don’t really need to pass
argc, since the final member ofargvis guaranteed to be a null pointer. But since you haveargc, you might as well pass it.If the function need not know about the program name, you can also decide to call it as