-1
int main(int argc, char const *argv[]) {
    if (argv[1] == '-n')
    {
        printf("Hello");
    }
}

When I run this, I'm getting a "warning- comparison between pointer and integer" error. How do I fix this?

I checked that argv[1] contains -n by printing.

3
  • 1
    argv is an array of strings. '-n' is a multi-character constant, not a string. Not that you use == to compare strings anyway. Commented Apr 20, 2018 at 3:35
  • Yes, that is a duplicate. Thanks for the answer! Commented Apr 20, 2018 at 3:37
  • -n isn't a character... Commented Apr 20, 2018 at 5:12

1 Answer 1

1

The argv[1] value represents a string, which is a character pointer type, while '-n' is a multi-byte character constant (an integer). That's why you're getting the "pointer and integer" mismatch.

You should be using string comparison functions here, such as:

// Make sure you HAVE an argument, then use string comparison to check.

if ((argc > 1) && (strcmp(argv[1], "-n") == 0)) {
    printf("hello"); 
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.