简体   繁体   English

了解C中的Char数组相等性

[英]Understanding Char Array equality in C

Sorry in advance for the ignorance. 提前抱歉无知。 I don't fully understand how to compare char arrays in C. I was originally comparing two char arrays in c with the simple == operator. 我不完全了解如何比较C中的char数组。我最初将c中的两个char数组与simple ==运算符进行比较。

So in a C function, I would do something like this. 所以在C函数中,我会做这样的事情。

char *a = "test";
char *b = "test";
if (a == b) ..do something

But I read that I should be using strcmp instead of == like this. 但我读到我应该使用strcmp而不是像这样的==

char *a = "test";
char *b = "test";
if (0 == strcmp(a, b)) ..do something

Which one is correct and why? 哪一个是正确的,为什么? What is the other one doing? 另一个人在做什么?

if (a == b)

Here you are comparing pointers and not the strings 在这里,您要比较指针而不是字符串

strcmp(a, b)

Here you are comparing strings 在这里你要比较字符串

Which one is correct and why? What is the other one doing?

Since there are 2 strings stored in different memory locations or if the same string is being stored there is possibility a==b comparing pointers doesn't make sense.What you want is to compare the contents of the locations the pointers are pointing to. 由于有两个字符串存储在不同的内存位置,或者如果存储相同的字符串,则有可能a==b比较指针没有意义。你想要的是比较指针所指向的位置的内容。 Which is done by strcmp() and this is the right way to compare the strings. 这是由strcmp()完成的,这是比较字符串的正确方法。

For example : 例如 :

#include <stdio.h>

int main(void) {
char *a = "test";
char *b = "test";

printf("%p\n %p",(void *)a,(void *)b);
    return 0;
}

The output is 输出是

0x8048540 
0x8048540

So both the pointers a and b are pointing to the same memory location a==b Note that here what we compare is not the actual characters in the string but just the pointers. 所以指针a和b都指向相同的内存位置a==b请注意,这里我们比较的不是字符串中的实际字符,而只是指针。

I ran the same code on another machine and the locations in which this string was stored was different. 我在另一台机器上运行相同的代码,并且存储该字符串的位置不同。

0x4005f8
0x4005fd

So now even though the strings might be same you see a != b . 所以现在即使字符串可能相同,你也会看到a != b Hence use strcmp() to compare strings. 因此使用strcmp()来比较字符串。

Doing this if (a == b) will compare Pointer values stored in a and b . if (a == b)执行此操作将比较存储在ab指针值。

So if you have a 所以,如果你有a

  a //say at some random address 1000 

or b b

  b //say at some random address 2000 

Is a==b ? a==b Now normally if the compiler is doing string pooling and if your string literals are exactly same then this may work in those cases - otherwise you have to do a character-by-character comparison as strcmp would i guess. 现在通常情况下,如果编译器正在进行字符串池化,并且如果你的字符串文字完全相同,那么这可能适用于那些情况 - 否则你必须按照strcmp进行逐字符比较。

String Literals are stored in contiguous memory locations in Text(Read Only) segment of the memory. 字符串文字存储在内存的文本(只读)段中的连续内存位置。

char *a = "test";
char *b = "test";
if (a == b) ..do something

Here you are comparing the address of the first elements of the arrays. 在这里,您要比较数组的第一个元素的地址。 This can results in equality as "test " being an String stored in text segment of the memory and *a and *b might point to that memory location. 这可以导致相等,因为"test ”是存储在存储器的文本段中的String,而*a and *b可能指向该存储器位置。

char *a = "test";
char *b = "test";
if (0 == strcmp(a, b)) ..do something

Here you are comparing each element of both arrays byte by byte till NULLCHAR(\\0) for any one of the input array is reached. 在这里,您将逐字节地比较两个数组的每个元素,直到达到任何一个输入数组的NULLCHAR(\\ 0)。

I would recommend to use strcmp because it compares the contents of the strings while == compares the address of the first elements in the strings. 我建议使用strcmp,因为它比较字符串的内容,而==比较字符串中第一个元素的地址。

Also, strcmp will tell you the relative ordering of the strings rather than simply if they are equal. 另外,strcmp会告诉你字符串的相对顺序,而不是简单地说它们是否相等。

if(a == b) will compare the addresses stored in a and b pointers. if(a == b)将比较存储在a和b指针中的地址。

strcmp(a, b) will compare character by character of the contents stored at a and b addresses. strcmp(a, b)将比较存储在a和b地址的内容的字符。 It returns 0 if both contents are same (case sensitive). 如果两个内容相同(区分大小写),则返回0。 otherwise difference in ASCII values of the characters 否则字符的ASCII值不同

if(*a == *b) will compare the first characters (ie char at 0th position) of both array. if(*a == *b)将比较两个数组的第一个字符(即第0个位置的char)。

Hope it helps !! 希望能帮助到你 !!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM