比较C中char []的相等性

时间:2021-06-15 23:00:34

I have two variables:

我有两个变量:

char charTime[] = "TIME";
char buf[] = "SOMETHINGELSE";

I want to check if these two are equal... using charTime == buf doesn't work.

我想检查这两个是否相等...使用charTime == buf不起作用。

What should I use, and can someone explain why using == doesn't work?

我应该使用什么,有人可以解释为什么使用==不起作用?

Would this action be different in C and C++?

这个行为在C和C ++中会有所不同吗?

5 个解决方案

#1


18  

char charTime[] = "TIME"; char buf[] = "SOMETHINGELSE";

C++ and C (remove std:: for C):

C ++和C(删除std :: for C):

bool equal = (std::strcmp(charTime, buf) == 0);

But the true C++ way:

但真正的C ++方式:

std::string charTime = "TIME", buf = "SOMETHINGELSE";
bool equal = (charTime == buf);

Using == does not work because it tries to compare the addresses of the first character of each array (obviously, they do not equal). It won't compare the content of both arrays.

使用==不起作用,因为它试图比较每个数组的第一个字符的地址(显然,它们不相等)。它不会比较两个数组的内容。

#2


3  

In c you could use the strcmp function from string.h, it returns 0 if they are equal

在c中你可以使用string.h中的strcmp函数,如果它们相等则返回0

#include <string.h>

if( !strcmp( charTime, buf ))

#3


1  

In an expression using == the names of char arrays decay into char* pointing to the start of their respective arrays. The comparison is then perform in terms of the values of the pointers themselves and not the actual contents of the arrays.

在使用==的表达式中,char数组的名称衰减为char *,指向它们各自数组的开头。然后根据指针本身的值而不是数组的实际内容执行比较。

== will only return true for two pointers pointing to the same location and false otherwise, even if they are pointing to two arrays with identical contents.

==仅对指向同一位置的两个指针返回true,否则返回false,即使它们指向具有相同内容的两个数组。

What you need is the standard library function strcmp. This expression evaluates as true if the arrays contain the same contents (up to the terminating null character which must be present in both arrays fro strcmp to work safely).

您需要的是标准库函数strcmp。如果数组包含相同的内容,则此表达式的计算结果为true(直到终止空字符,两个数组中必须存在,以便strcmp安全地工作)。

strcmp(charTime, buf) == 0

#4


0  

Check them in a for loop. Get the ASCII numbers for each char once they change they're not equal.

在for循环中检查它们。获取每个字符的ASCII编号,一旦它们发生变化,它们就不相等。

#5


0  

You are checking the identity charTime and buf. To check the equality, loop over each character in one array and compare them with the related character in the other array.

您正在检查身份charTime和buf。要检查相等性,请遍历一个数组中的每个字符,并将它们与另一个数组中的相关字符进行比较。

#1


18  

char charTime[] = "TIME"; char buf[] = "SOMETHINGELSE";

C++ and C (remove std:: for C):

C ++和C(删除std :: for C):

bool equal = (std::strcmp(charTime, buf) == 0);

But the true C++ way:

但真正的C ++方式:

std::string charTime = "TIME", buf = "SOMETHINGELSE";
bool equal = (charTime == buf);

Using == does not work because it tries to compare the addresses of the first character of each array (obviously, they do not equal). It won't compare the content of both arrays.

使用==不起作用,因为它试图比较每个数组的第一个字符的地址(显然,它们不相等)。它不会比较两个数组的内容。

#2


3  

In c you could use the strcmp function from string.h, it returns 0 if they are equal

在c中你可以使用string.h中的strcmp函数,如果它们相等则返回0

#include <string.h>

if( !strcmp( charTime, buf ))

#3


1  

In an expression using == the names of char arrays decay into char* pointing to the start of their respective arrays. The comparison is then perform in terms of the values of the pointers themselves and not the actual contents of the arrays.

在使用==的表达式中,char数组的名称衰减为char *,指向它们各自数组的开头。然后根据指针本身的值而不是数组的实际内容执行比较。

== will only return true for two pointers pointing to the same location and false otherwise, even if they are pointing to two arrays with identical contents.

==仅对指向同一位置的两个指针返回true,否则返回false,即使它们指向具有相同内容的两个数组。

What you need is the standard library function strcmp. This expression evaluates as true if the arrays contain the same contents (up to the terminating null character which must be present in both arrays fro strcmp to work safely).

您需要的是标准库函数strcmp。如果数组包含相同的内容,则此表达式的计算结果为true(直到终止空字符,两个数组中必须存在,以便strcmp安全地工作)。

strcmp(charTime, buf) == 0

#4


0  

Check them in a for loop. Get the ASCII numbers for each char once they change they're not equal.

在for循环中检查它们。获取每个字符的ASCII编号,一旦它们发生变化,它们就不相等。

#5


0  

You are checking the identity charTime and buf. To check the equality, loop over each character in one array and compare them with the related character in the other array.

您正在检查身份charTime和buf。要检查相等性,请遍历一个数组中的每个字符,并将它们与另一个数组中的相关字符进行比较。