unable to pass argv[2] to a function

Hi,

When I try to pass to a function the value of argv directly, I am unable to do so.
Code snippets of what I am trying to do is as below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

//The function init_variables definition

init_variables(char* f_dataset, char* f_input)
{
 ...
 ...
}

int main(int argc, char* argv[])
{
  init_variables(argv[1], argv[2]);
}


The problem I see is that during runtime, The second argument (argv[2]), does not get passed to the init_variables function while the first argument (argv[1]) does. I checked this using VS2010 debugger. I can see during execution that the value of argv[2] is read correctly though.

Can anyone tell me a reason why this might be happening??

I solved the problem (a small quick-fix) by converting the argument to strings, as in the following

1
2
3
4
5
6
7
8
9
10
11
12
13

//init_variables function definition
void init_variables(string f_dataset, string f_input)
{ 
 ...
 ...
}

int main(int argc, char* argv[])
{
	init_variables(std::string(argv[2]), std::string(argv[4]));
	return 0;
}
Last edited on
You did not solve the problem because you did not get answer on your question. Though in my opinion none problem existed. It was only your fantasy.
Well, In my opinion, the problem exists because I can see it during runtime when I check the values :).

By solving I meant, I manged a quick-fix, to move ahead. I also mention what my quick-fix was, so that people who have an idea about this, might be able to compare and tell me what exactly is the difference.

Thanks for the response!

Well, when run this program and say what will be displayed


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

void init_variables( char* f_dataset, char* f_input )
{
   std::cout << "f_dataset = " << f_dataset
                 << ", f_input = " << f_input << std::endl; 
}

int main(int argc, char* argv[])
{
   if ( argc >= 3 ) init_variables( argv[1], argv[2] );
   else std::cout << "two arguments shall be specified with the main\n";
}


And next time please do not make up a problem.
Last edited on
Thanks for the effort... let me tell you, ridiculous as this sounds! (I'd be as unbelieving as you are).. but I am extremely confused here, and it'd help a lot if someone could tell me what could be the problem here


but please do take a look at the following two screen shots

I agree your code totally works, and the values do get passed into init variables... and here is a screen shot of the runtime value

When the code

1
2
std::cout << "f_dataset = " << f_dataset
                 << ", f_input = " << f_input << std::endl;


is not commented

https://docs.google.com/open?id=0BxsLJCFQ4I0aUVR1dWtzTFNnNFE


and when it is commented here is a screen shot of the runtime value passed!!

https://docs.google.com/open?id=0BxsLJCFQ4I0ab1YwMjlXTktmV28

it does not act the same way!!! I know this sounds ridiculous... but I just hope that somebody here can find an explanation here!!


Would it be VS2010's problem??

vlad, thanks for asking me to run that code, now I feel there is some answer that I should be able to find here!!!
Last edited on
I do not open uknown references.
So show the code where you comment the output statement and what is wrong with this. Because the function contains only this output statement. I do not understand what you are awaiting.
Could it be because of optimizations done by the compiler? Maybe the compiler sees that the parameters will never be used inside the function so it just don't initialize the parameters.
Peter, thanks for bringing up a very logical point... now that is very possible, since I have not yet completed that part of the code that uses the second argument. I will do that and see if that solves the issue, and let you know!

Thanks indeed!
The behavior was indeed due to compiler optimization! In fact I saw a lot more cases where VS2010 optimization in fact made it difficult for me to do a runtime debugging and so had to disable optimization!

Thanks for all the responses!
Topic archived. No new replies allowed.