understanding a char pointer to array in main(int argc, char *argv[])

The following parameter list defines and integer argc and a char pointer to and array. I know that argc passes the number of command line arguments and argv[] is a character array that holds a string within each array element. I am having problems understanding the syntax. And array name is a pointer to some array. Is this a char pointer to a pointer I don't understand. I would like to write a routine that would store character string in each array element. IM Not wanting to change the main routine (parameter list it does it already)to store character string in each element but a routine that's called by main. I had to edit post to make this clear.


int main(int argc, char *argv[])
Last edited on
Yes it is, you could have written it as

int main(int argc, char **argv)
This still means the same thing
Smac is correct.

I have to wonder why you want to write something into that particular array? The purpose of argv is to hold the text of the command-line operation that was used to run the program. When the program starts, argc is automatically set to the number of items on the command line, and argv is populated with the strings that made up that command line.

Why do you need to change the contents of it?
One use case for modifying argv is when you're going to launch another task, to which you're forwarding your commandline arguments with some small tweaks. it's easier to modify argv than to prepare a new set of arrays. It's very rare, of course.
It also depends on what you mean by "change the contents of it". Some things you can change, some things you can't, and some things you shouldn't.

I would like to write a routine that would store a character string in each array element.

Don't. You are asking for trouble.

Just use a vector of strings (std::vector <std::string>).

1
2
3
4
5
int main( int argc, char** argv )
  {
  vector <string> args( argv, argv + argc );

  //now modify args to your heart's content 


Hope this helps.
Topic archived. No new replies allowed.