-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.

xskxzr
  • 12,442
  • 12
  • 37
  • 77
Zack King
  • 31
  • 7

1 Answers1

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"); 
}
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Arnabjyoti Kalita
  • 2,325
  • 1
  • 18
  • 31