C++ 比较C++中char数组的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15050766/
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-08-27 18:56:44  来源:igfitidea点击:

Comparing the values of char arrays in C++

c++arrayschar

提问by yrazlik

I have problem about doing something in my program. I have a char[28] array keeping names of people. I have another char[28] array that also keeps names. I ask user to enter a name for the first array, and second arrays reads names from a binary file. Then i compare them with == operator, But even though the names are the same, their values look different when i debug it. Why is this the case? How can i compare these two? My sample code is as follows:

我在我的程序中做某事时遇到问题。我有一个保存人名的 char[28] 数组。我还有另一个 char[28] 数组,它也保留了名称。我要求用户输入第一个数组的名称,第二个数组从二进制文件中读取名称。然后我将它们与 == 运算符进行比较,但即使名称相同,它们的值在我调试时看起来也不同。为什么会这样?我如何比较这两个?我的示例代码如下:

int main()
{
    char sName[28];
    cin>>sName;      //Get the name of the student to be searched

      /// Reading the tables

    ifstream in("students.bin", ios::in | ios::binary);

    student Student; //This is a struct

    while (in.read((char*) &Student, sizeof(student)))
    {
    if(sName==Student.name)//Student.name is also a char[28]
    {
                cout<<"found"<<endl;
        break;
    }
}

采纳答案by juanchopanza

Assuming student::nameis a chararray or a pointer to char, the following expression

假设student::name是一个char数组或指向 的指针char,则以下表达式

sName==Student.name

compares pointers to char, after decaying sNamefrom char[28]to char*.

从到char衰减后比较 到 的指针。sNamechar[28]char*

Given that you want to compare the strings container in these arrays, a simple option is to read the names into std::stringand use bool operator==:

鉴于您想比较这些数组中的字符串容器,一个简单的选择是将名称读入std::string并使用bool operator==

#include <string> // for std::string

std::string sName;
....

if (sName==Student.name)//Student.name is also an std::string

This will work for names of any length, and saves you the trouble of dealing with arrays.

这将适用于任何长度的名称,并为您省去处理数组的麻烦。

回答by nvoigt

You can compare char arrays that are supposed to be strings by using the c style strcmpfunction.

您可以使用 c 样式strcmp函数比较应该是字符串的字符数组。

if( strcmp(sName,Student.name) == 0 ) // strings are equal

In C++ you normally don't work with arrays directly. Use the std::stringclass instead of character arrays and your comparison with == will work as expected.

在 C++ 中,您通常不直接使用数组。使用std::string类而不是字符数组,您与 == 的比较将按预期工作。

回答by Roee Gavirel

The problem is in if(sName==Student.name)which basically compares the address of the arrays, not their values.
Replace it with (strcmp(sName, Student.name) == 0)

问题在于if(sName==Student.name)它基本上比较了数组的地址,而不是它们的值。
将其替换为(strcmp(sName, Student.name) == 0)

But in general, you are working on C++, not C, I would advise working with std::string which will make this much simpler.

但总的来说,您正在使用 C++,而不是 C,我建议使用 std::string ,这将使这更简单。

回答by pahoughton

if( sName == Student.name ) is comparing the addresses

if( sName == Student.name ) 正在比较地址

if( strcmp( sName, Student.name ) == 0 { 
  / * the strings are the same */
}

Be careful with strcmp though

不过要小心 strcmp

回答by habib

You can write code for your own char array compare function. Let's start

您可以为自己的字符数组比较函数编写代码。开始吧

//Return 0 if not same other wise 1
int compare(char a[],char b[]){
    for(int i=0;a[i]!='##代码##';i++){
        if(a[i]!=b[i])
            return 0;
    }
    return 1;
}