C语言 C - 比较字符串文字与字符数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18026445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:03:25  来源:igfitidea点击:

C - Comparing string literal with character array

carraysstringcharacterstrcmp

提问by Irina

I am new to C and am still a bit confused about how to use strings via character arrays.

我是 C 新手,对如何通过字符数组使用字符串仍然有些困惑。

In my C program, I am accepting commands from the user:

在我的 C 程序中,我接受来自用户的命令:

char command[20];
scanf("%s",command);

Of course, afterwards I want to figure out what command they typed (something similar to: "if (command == "hello"), then do something"). I know this is not possible in C because I am comparing a string literal to a character array, but what would be a good way to it? I have tried using strcmp(command, "hello")and still got errors.

当然,之后我想弄清楚他们输入了什么命令(类似于:“ if (command == "hello"),然后做某事”)。我知道这在 C 中是不可能的,因为我将字符串文字与字符数组进行比较,但是有什么好的方法呢?我试过使用strcmp(command, "hello"),仍然有错误。

Any advice you can provide would be very appreciated. Thank you!

您可以提供的任何建议将不胜感激。谢谢!

采纳答案by gravitas

strcmp returns 0 when the strings are the same. I have code that uses strcmp comparing character arrays to string literals, and I was quite confused when it wasn't working. Turns out it was wrong for me to assume it would return 1 when the string are the same!

当字符串相同时,strcmp 返回 0。我有使用 strcmp 将字符数组与字符串文字进行比较的代码,当它不起作用时我很困惑。事实证明,当字符串相同时,我认为它会返回 1 是错误的!

Maybe you've made the same mistake?

也许你也犯过同样的错误?

回答by HymanCColeman

I have written a complete version of what I think you are trying to do:

我已经写了一个我认为你正在尝试做的完整版本:

    #include <string.h>
    void main()
    {

       char command[20];
       scanf("%s",command);

       // command and "hello" can be less than, equal or greater than!
       // thus, strcmp return 3 possible values
       if (strcmp(command, "hello") == 0)
       {
          printf("\nThe user said hello!");
       }

    }

Several people have commented about using scanfand they are correct, except that a new programmer has to start somewhere in learning this stuff, so don't feel too bad we are all learning...

有几个人评论了使用scanf,他们是正确的,除了一个新的程序员必须从某个地方开始学习这些东西,所以不要感觉太糟糕我们都在学习......

Hope this helps.

希望这可以帮助。

回答by spartygw

I think this is a perfect starting point for you:

我认为这对您来说是一个完美的起点:

http://www.wikihow.com/Compare-Two-Strings-in-C-Programming

http://www.wikihow.com/Compare-Two-Strings-in-C-Programming

It's probably written at the right level for you. Good luck and welcome to stackoverflow!

它可能以适合您的级别编写。祝你好运,欢迎来到 stackoverflow!

回答by qunying

When talking about string in C, it normally takes two forms: 1. a character array, 2. a character pointer. Most of the time, they are interchangeable. For example:

在 C 中谈论字符串时,通常有两种形式:1. 字符数组,2. 字符指针。大多数时候,它们是可以互换的。例如:

char *cmd_ptr = "command1";
char cmd_array[20] = "command2";
printf ("cmd1: %s cmd2: %s\n", cmd_ptr, cmd_array);

The main difference for the above definition is that for cmd_ptryou could not change its content like cmd_ptr[0] = 'a';for cmd_arrayyou could change any element in the array.

对于上述定义的主要区别是,cmd_ptr你不能改变它的内容,如cmd_ptr[0] = 'a';cmd_array你可以在阵列中的任何改变元素。

But you could do cmd_ptr = cmd_array;then you could make changes through cmd_ptras it points to the same location as cmd_array.

但是您可以这样做,cmd_ptr = cmd_array;然后您可以进行更改,cmd_ptr因为它指向与cmd_array.