char *argv[] initialization

Hi,

I am trying to initialize "argv" with the argument I want for this program while I am working on the rest of it.
You will understand my problem by reading my trial (which does not compile, naturally):

1
2
3
4
5
6
7
8
9
10
11
12
int main(int argc, char *argv[]){
	//if (argc != 6){ // argc should be 6 for correct execution
	//	cout << "usage: " << argv[0] << " <path> <temp> <water> <time_step> <iteration>\n" << endl;
	//	return 1;
	//}
	if (argc != 6){
		char* argv[1]={"C:\Users\Robin\Documents\pipe_GWB9_PlugIn_2008\Debug"}; 
		char* argv[2]={"24.9"};
		char* argv[3]={"-999.999"};
		char* argv[4]={"1"};
		char* argv[5]={"1"};
	}


Could you then help me initializing the variables argv[i], please?
I am still struggling with the arrays of pointers...
Thank you

Sky
The problem is best solved as follows:
1
2
3
4
5
6
7
8
vector<string> args(argv,argv+argc);
if (args.size()!=6)
{
    args.resize(6);
    args[1]="C:\Users\...";
    args[2]="24.9";
    ...
}
Both parameters of main() are modifiable, but the array referenced by the second parameter is not resizable. If someone called your program with one argument, you can't even access argv[3] and up. To substitute a different set of arguments, you have to repoint the whole argv at your own array of pointers, each pointing at your own string. And don't forget to provide the terminating entry (argv[argc]==NULL)

Or just get into the habit of converting main's arguments into a vector<strings> as Athar suggests, in which case this is trivial.
Last edited on
Thank you so much for your answers.

The <vector> trick seems to be working well until I use the following line to use some of the arguments as char array (I need this "temperature = 24.9" in this format):
char cstr_temp[200]; sprintf(cstr_temp,"temperature = %s", argv[2]);

Do you know why and how argv[2] could fit in this?
Thanks

Sky
PS: If I leave this line like this, it displays "temperature = 8[C"
You aren't supposed to use sprintf in C++ in the first place, but you can get a C string from a C++ string using c_str().
Ok thanks, and is there any possibility to initialize something like this:
argv[1]="C:\\";
(which doesn't work :s), to something like that, directly on the same line:
argv[1]=("C:\\").c_str();
?

Thanks again

Sky
Last edited on
What are you talking about?
args[1]="C:\\";
will work just fine, as long as there are enough elements in the vector.

("C:\\").c_str() is nonsense - String literals are already C strings.
I am sorry I started to be confusing, and to get confused with what I wrote... Here we go again:



The thing is that I need to be able to use args[2] as such, in
char cstr_temp[200]; sprintf(cstr_temp,"temperature = %s", argv[2]) where argv[2]) is a char array.
(I am not allowed to change this part of the code)

And to do that, I need args[2] to be transformed into a char array, that is why I wanted to find an equivalent to args[2]=("24.9").c_str();. But if THIS is not possible,
Do you know why char* argv[2]=args[2].c_str(); doesn't seem to wanna work?
Last edited on
Because c_str() returns a pointer to const char. args[2].c_str() gives you a C string.
But this is pointless - you need to review the basics first (in particular std::string and C strings) before you touch any of this again.
Topic archived. No new replies allowed.