argv voes

Somebody please help me with this.
I'm trying to check if a certain flag was passed to the program from
command line or not.
Basically, I'm trying to do something like if(argv[1] == 'd')
Of course it doesn't work!
I've tried everything I could come up with and it either gives me a warning
about comparison between pointer and integer or I get a segmentation fault.

I've tried strcmp and strncmp. Doesn't help either.
Last edited on
argv[1] will be char*, like a string literal "hello". You're trying to compare a string to a character, it doesn't like this.
But yes you will still need to use strcmp for this purpose, as == cannot be overloaded to do this unless you convert argv[1] into an std::string first, and even then you'd need to change it to "d" instead of 'd' either way, since apparently std::string doesn't have the overloads built-in for this either.

tl;dr Use strcmp and use "d" instead of 'd'
edit:
if (strcmp(argv[1], "d") == 0)
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
int main(int argc, char* argv[])
{
    if(argc > 1)
    {
        if(argv[1][0] == '-')
        {
            switch(argv[1][1])
            {
                case 'o': break; // argv[2] would be the text following the flag switch
            }
        }
    }
}



./program -o Something
Last edited on
It works! Thank you, thank you, thank you!

One thing I don't understand though..
I did try looking for a character in the second dimension like this argv[1][0]=='d'.
But I wasn't checking for the number of arguments. Could this be why I was getting segmentation
faults?

Thank you.
You are very welcome,

It could be, I wouldn't be able to know for sure without the code. Sorry.
Topic archived. No new replies allowed.