0

I am writing code that is taken from the command line. The user will input lets say for example ./a.out -l 2 4 6 into the command line. The goal here is to loop through the array and see if either '-l' or '-s' appear. If '-l' appears it makes x = 1 , if '-s' x = 2 if neither x = 0. Right now the issue being thrown is comparison between pointer and integer on line 7 and 12. Also a multi-character character constant on line 12, which I'm not sure why its being thrown when line 9 is okay. How would I change my if statements to fix the issues being thrown? My code is as follows:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]){

  int x;

  for(; *argv != '\0'; argv++){
    if(*argv == '-l'){
      x = 1;
  }
    else if(*argv == '-s'){
      x = 2; 
  } 
    else{
     x = 0;
  }
}
  printf("%d",x);
  return 0;
}
3
  • 2
    Possible duplicate of How to compare pointer to strings in C Commented Jul 1, 2016 at 16:08
  • Same answer slightly different question, I am not comparing two strings but looking for a char from a string input that is now in an array Commented Jul 1, 2016 at 16:14
  • 1
    Yes you are trying to compare two strings... If not, why did you write *argv == '-s'? Commented Jul 1, 2016 at 16:17

2 Answers 2

2

Strings are specified with double quotes, not single quotes which are used for characters. Also, you can't use == to compare strings. Use strcmp for that:

if(strcmp(*argv,"-l") == 0){

...

if(strcmp(*argv,"-s") == 0){

Your output will also not be as you expect. You overwrite x each time you check the next parameter, so the result will depend only on the last one. You need to break out of the loop when one of the two conditions is met.

Sign up to request clarification or add additional context in comments.

1 Comment

my output is x = 0 which is not the correct when I do ./a.out -l 2 4 6 any ideas?
0

Apart from the errors pointed out by others, you might at some point want to do proper command line parsing. On a POSIX system , this is done with the getopt() library function declared in the unistd.h header (this is not "C standard" C code).

I've extended your requirements with an option -x that takes an integer argument that says what x should be set to:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    int x = 0; /* default zero for x */
    int i, tmp;
    int opt;

    char *endptr;

    /* the ':' in the string means "the previous letter takes an argument" */
    while ((opt = getopt(argc, argv, "lsx:")) != -1) {
        switch (opt) {
        case 'l':
            x = 1;
            break;
        case 's':
            x = 2;
            break;
        case 'x':
            /* validate input, must be integer */
            tmp = (int)strtol(optarg, &endptr, 10);
            if (*optarg == '\0' || *endptr != '\0')
                printf("Illegal value for x: '%s'\n", optarg);
            else
                x = tmp;
            break;
        case '?': /* fallthrough */
        default:
            /* call routine that outputs usage info here */
            puts("Expected -s, -l or -x N");
        }
    }

    argc -= optind; /* adjust */
    argv += optind; /* adjust */

    printf("x = %d\n", x);

    puts("Other things on the command line:");
    for (i = 0; i < argc; ++i)
        printf("\t%s\n", argv[i]);

    return EXIT_SUCCESS;
}

Running it:

$ ./a.out -l
x = 1
Other things on the command line:

$ ./a.out -s
x = 2
Other things on the command line:

The last option "wins":

$ ./a.out -s -x -78
x = -78
Other things on the command line:

Here, -s is last:

$ ./a.out -s -x -78 -ls
x = 2
Other things on the command line:

$ ./a.out -s -q
./a.out: illegal option -- q
Expected -s, -l or -x N
x = 2
Other things on the command line:

Parsing stops at the first non-option:

$ ./a.out -s "hello world" 8 9 -x 90
x = 2
Other things on the command line:
    hello world
    8
    9
    -x
    90

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.