2

If the first character of the first argument == "-" then enter the if statement. The error I get is "passing argument 1 of ‘strcmp’ makes pointer from integer without a cast" I have also tried this with fgetc, written a little differently, but still get this error. If I cast it I get a core dump. If the argument contains a dash it would be followed by numbers inside the same argument so I cannot just look at argv[1] as a single char, if I could the condition would be as simple as: if (strcmp(argv[1], "-")==0)

Function

int main (int argc, const char *argv[]){
    const char *test = argv[1];
    if (strcmp(test[0],"-")==0)
        {
          printf("saw there was a dash");
        }
    return 0;}

Thank you for your help

JRX
  • 35
  • 1
  • 1
  • 7
  • If you are on a *nix (incl. Linux and Mac OS X) system, you might as well use [`getopt(3)`](http://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html#Example-of-Getopt) – nodakai Feb 24 '14 at 05:13

1 Answers1

7

strcmp() is to compare strings, that is, null terminated char arrays. But in here you are comparing two char, this will do:

if (test[0] == '-')

Note the single quotes in '-', that's a char literal.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294