Testing a value that may not exist

Hello! :)

I want to use the arguments of main. For now, I did this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main(int argc, char **argv)
{
//get the argmuments
  std::vector<std::string> args;
  for(int i = 0; i < argc; i++)
    {
      args.push_back(argv[i]);
    }

  if(args.size() > 2 || argv[1]=="-h")
    {
      usage(args[0].c_str());
      exit(0);
    }
  ...


but gcc returns a warning for this : "warning: comparison with string literal results in unspecified behaviour".
But if I try to compare with my string vector "args", it works (no warning) when there is an argument but I got a segmentation fault when there is not (and that's logic).

I thought about something like :
1
2
3
4
5
if(args.size()>1){
      if(args[1]=="-h"){
         ...
      }
   }


But I really don't like it ^^
So I just wonder how to do this properly, without any warning.

Thanks :)
But I really don't like it ^^
That's the only way you have to see if you have any argument

argv[1]=="-h" doesn't work because you can't compare char arrays like that, you need to use functions like strcmp, std::strings support the == operator
What Bazzy said. You probably just have the typo in your line 10. You mean "args[1]" not "argv[1]". Argv is the original list, not your vector, so it contains char* and not std::strings. You can only use the comfortable =="-h" with strings, not with char*. Hence the warning/segfault.

and another one: || is OR, so your if statement means "do this if either the size is big OR the second argument (of a possible empty array) is '-h'"

You probably want to say: "The size have to be enough AND the second argument is -h", right? ;-). && is "AND". Your second approach of cascading the if-statements is exactly the same as the && - thing. (and see? You even correctly used the args and not argv ;-)

Ciao, Imi.
=> imi
Yes, I know for && and ||, it was just to test arguments so I did not care. But I agree it would be more logical :)
Line 10 is argv[1] because if I write args[1], the segfault appears when there is no argument (since in this case, the size of my vector args is 1, so there is no item at [1]). But with argv, there is no segfault even with no argument... I don't know why ^^

=> Bazzy
Ah I did not know strcmp. That should work :)

Thank you both!
But with argv, there is no segfault even with no argument... I don't know why ^^

Do you mean like this?
1
2
3
4
5
6
7
int main(int argc, char **argv)
{
    if( !strcmp(argv[1],"-h") )
    {
        //...
    }
}

Are you sure?
Even if you don't get a segfault doesn't mean that it's good. You MUST check that the argument exists in any case
no no, just as I wrote :

1
2
if(args.size() > 2 || argv[1]=="-h")     => no segfault
if(args.size() > 2 || args[1]=="-h")   => segfault


That's why I do not understand :)
But now, I will check as you said.

Thanks again!
if(args.size() > 2 || args[1]=="-h") => segfault

You should get the segfault if argc (and hence args.size()) is smaller or equal to 2. Becaue you are accessing an array out of bound.

The reason you don't get a segfault when you use argv is only by chance as it's still an out of bound access (OOB-access doen't always give segfaults - only if you are lucky as you detect your mistake. Sometimes you just accessing wrong memory and you get strange and unexpected programms.)

Ciao, Imi.
Ok. I did not know there was a "lucky part" :)

Thank you very much for your answers.
Cya
Topic archived. No new replies allowed.