Warning: comparison with string literals results in unspecified behaviour

后端 未结 7 2438
终归单人心
终归单人心 2020-12-05 01:52

I am starting a project of writing a simplified shell for linux in C. I am not at all proficient with C nor with Linux that\'s exactly the reason I decided it would be a goo

7条回答
  •  不知归路
    2020-12-05 02:28

    1. clang has advantages in error reporting & recovery.

      $ clang errors.c
      errors.c:36:21: warning: result of comparison against a string literal is unspecified (use strcmp instead)
              if (args[i] == "&") //WARNING HERE
                          ^~ ~~~
                  strcmp( ,     ) == 0
      errors.c:38:26: warning: result of comparison against a string literal is unspecified (use strcmp instead)
              else if (args[i] == "<") //WARNING HERE
                               ^~ ~~~
                       strcmp( ,     ) == 0
      errors.c:44:26: warning: result of comparison against a string literal is unspecified (use strcmp instead)
              else if (args[i] == ">") //WARNING HERE
                               ^~ ~~~
                       strcmp( ,     ) == 0
      

      It suggests to replace x == y by strcmp(x,y) == 0.

    2. gengetopt writes command-line option parser for you.

提交回复
热议问题