Comparing the values of char arrays in C++

后端 未结 5 1933
广开言路
广开言路 2021-01-02 02:38

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

5条回答
  •  误落风尘
    2021-01-02 03:15

    Assuming student::name is a char array or a pointer to char, the following expression

    sName==Student.name
    

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

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

    #include  // 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.

提交回复
热议问题