1

I executed the below program as: ./aout w.

#include<iostream>
using namespace std;

int main(int argc, char** argv)
{
    if (argv[1] == "w")
    {
        cout << "this was worked";
    }
    else
    {
        cout << "this did not worked";
    }
}

OUTPUT: this did not worked.
I tried executing: if(&argv[1] == "w") if(argv[1]=='w')

Ghasem Ramezani
  • 2,683
  • 1
  • 13
  • 32
  • 4
    `argv[1]` is a pointer. `"w"` is an array which decays to a pointer. Neither pointer will be equal, ever. For C-style null-terminated string you need to use [`std::strcmp`](https://en.cppreference.com/w/cpp/string/byte/strcmp). Or convert one of the string to a [`std::string`](https://en.cppreference.com/w/cpp/string/basic_string). – Some programmer dude Sep 02 '21 at 15:20
  • Use `strncmp` for `char[]`, `char*` comparison. – Ghasem Ramezani Sep 02 '21 at 15:21
  • 2
    I usually convert the arguments (omitting the first one, since it isn't really an argument) into a vector of string objects as the first thing in main. `auto args = vector(argv+1, argv+argc);` Much easier to work with `args` in C++. – Eljay Sep 02 '21 at 15:41
  • 1
    Why do you think you're comparing a pointer with an int? Which expression do you believe to be an int type? – Useless Sep 02 '21 at 15:41
  • 2
    @Eljay I recommend a span of string views to skip unnecessary dynamic allocation. – eerorika Sep 02 '21 at 15:43
  • @Useless I assume the `if(argv[1]=='w')` attempt would give such a warning. – Some programmer dude Sep 02 '21 at 15:43

2 Answers2

3

If you compare arrays and pointers using the equality operator, then you are comparing whether they are the same array or point to the same object. You aren't comparing the content of the arrays or pointed elements. Since argv[1] points to a different array than the string literal, they compare unequal regardless of their content.

tried executing: if(&argv[1] == "w")

Now, you're comparing char** to a const char* (after array decay). Comparing pointers of unrelated types doesn't make much sense.

if(argv[1]=='w').

Now, you're comparing char* to char. Comparing a pointer to a char doesn't make much sense.


To compare the content of two strings, A good solution is to use a string view as either or both of the operands. It's a small change:

 using namespace std::literals;
 if (argv[1] == "w"sv)
eerorika
  • 232,697
  • 12
  • 197
  • 326
3

The line

if (argv[1] == "w")

is equivalent to

char const* p1 = argv[1];
char const* p2 = "w";
if ( p1 == p2 )

i.e. you are comparing two pointers, not the strings that the pointers point to.

You can use std::strcmp. However, since you are using C++, you might as well use std::string.

if (std::string(argv[1]) == "w")

If you are able to use C++17, you may use std::string_view too.

if (std::string_view(argv[1]) == "w")
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • strcmp doesnot worksit works same as argv[1]=="w" – Chintu sharma Sep 03 '21 at 08:47
  • Thanks` if (std::string(argv[1]) == "w")` was effective does std::string() function changes char** to char* – Chintu sharma Sep 03 '21 at 08:55
  • `if (std::strcmp(argv[1], "w") == 0)` should work. – R Sahu Sep 04 '21 at 04:27
  • `std::string(argv[1])` constructs a `std::string` object. It does not change `char**` to `char*`. This question makes me think that you should spend some time learning the basics of the language and the standard library from a [good textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – R Sahu Sep 04 '21 at 04:30