Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to change argv or do I need to create an adjusted copy of it?

My application has potentially a huge number of arguments passed in and I want to avoid the memory of hit duplicating the arguments into a filtered list. I would like to filter them in place but I am pretty sure that messing with argv array itself, or any of the data it points to, is probably not advisable. Any suggestions?

like image 511
ojblass Avatar asked Jun 08 '09 05:06

ojblass


People also ask

Can you change argv?

Once argv has been passed into the main method, you can treat it like any other C array - change it in place as you like, just be aware of what you're doing with it.

Can you add to argv?

Short Answer: You can't. The way C organizes memory does not allow it. In your function int main(int, char**) you get two variables passed.

Can you write to argv?

The syntax char** argv declares argv to be a pointer to a pointer to a character, that is, a pointer to a character array (a character string)--in other words, an array of character strings. You could also write this as char* argv[] .

What is the purpose of argv?

The first element of the array, argv[0] , is a pointer to the character array that contains the program name or invocation name of the program that is being run from the command line. argv[1] indicates the first argument passed to the program, argv[2] the second argument, and so on.


1 Answers

The C99 standard says this about modifying argv (and argc):

The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.

like image 57
Michael Burr Avatar answered Sep 18 '22 15:09

Michael Burr