C ++ editing elements char * argv []

You all know this feature:

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

I want to write a Linux command line interface for my program, which is usually executed using getopt_long()

My program will be executed from the command line as follows:

 pop3 get --limit 25 --recent 

Therefore, argv[] will include pop3 as the name of its program, and the rest are treated as parameters. I want to remove pop3 from my string and set the first token after it as the first element of the array. Is there a way to do this other than a loop?

+5
source share
1 answer

argv pointer argv and reduce argc . Example:

 int main(int argc, char *argv[]) { argc--; argv++; return 0; } 

This works because when you increase argv , you still have the previous data in memory, just the base address of argv has increased. And you reduce argc , because now you have one argument less.

+11
source

All Articles