Compare two equal arrays in c, but the output shows unequal

After a long break, I returned to C, but got confused even on some simple questions. So there it is.

Here is a simple code:

#include<stdio.h> int main() { char str1[]="hello"; char str2[]="hello"; if(str1==str2) printf("equal"); else printf("unequal"); } 

Conclusion: unequal

but when i tried this it worked

  char *str1="hello"; char *str2="hello"; 

Output equals

Please if someone can provide a detailed explanation. Can someone tell me what exactly the C99 standard says about the situation?

+4
source share
7 answers

When you do == with pointers (which means str1 and str2 in both cases 1 ), all you do is compare the two addresses to see if they are the same. When you do

 char str1[]="hello"; char str2[]="hello"; 

You create two arrays on the stack that contain "hello" . They, of course, are in different places of memory therefore str1 == str2 - false . It looks like

 char str1[6]; str1[0] = 'h'; str1[1] = 'e'; str1[2] = 'l'; str1[3] = 'l'; str1[4] = 'o'; str1[5] = '\0'; // and the same thing for str2 

When you do

 char *str1="hello"; char *str2="hello"; 

You create two pointers to the global "hello" data. The compiler, seeing that these string literals are the same and cannot be changed, will force the pointers to indicate the same address in memory, and str1 == str2 - true .

To compare the contents of two char* s, use strcmp :

 // strcmp returns 0 if the two strings are equal if (strcmp(str1, str2) == 0) printf("Equal"); else printf("Not equal"); 

This is roughly equivalent

 char *a, *b; // go through both strings, stopping when we reach a NULL in either string or // if the corresponding characters in the strings don't match up for (a = str1, b = str2; *a != '\0' && *b != '\0'; ++a, ++b) if (*a != *b) break; // print Equal if both *a and *b are the NULL terminator in // both strings (ie we advanced a and b to the end of both // strings with the loop) if (*a == '\0' && *b == '\0') printf("Equal"); else printf("Not equal"); 


1 In the char* version, this is true. In the char[] version, str1 and str2 are arrays, not pointers, but when used in str1 == str2 they break up into pointers to the first elements of the arrays, so they are equivalent to pointers in this scenario.
+10
source

You are comparing pointers with strings, not with the strings themselves, but since they are two different strings, they are not equal to "==".

If you are comparing strings in C, you will need to use strcmp (str1, str2) a'la

 if(strcmp(str1, str2) == 0) { 

A word of caution; if you declared strings as char * instead of char [], some compilers would actually demonstrate your comparison as equal, since the compiler implements two strings equal and folds them into one. This means that both of them point to the same line and the comparison of pointers will be true.

Bitten by this once, never again, dash the VAX compiler ... :)

+2
source

Strings ( char pointers) in C must be compared with strcmp() or one of its sister functions.

You shouldn't work correctly (i.e. you should always use strcmp() when comparing strings), but the reason the last one probably worked was because in the second example you have one line in memory (maybe) which is hello, and both str1 and str2 ultimately point to it as optimizations.

At first, however, the creation of two separate arrays of characters is actually required, so they are not the same pointer.

0
source

You are not comparing arrays; you are comparing pointers to data. In the first case, when you declare a char [], you get two different arrays at two addresses, so they are not equal. In the second case, you get a constant array, so both pointers can get the same address, since the const array cannot be changed.

0
source

The problem is that when using

* str1 = "hello"; * str2 = "hello";

The compiler can optimize this to use the same memory space, and therefore str1 and str2 will both point to the same memory space. However, when using array notation, the compiler most likely creates two arrays in memory, and therefore pointers point to different memory cells.

when using ==, it checks for equality of pointers, not strings.

0
source

In your first case, you are comparing pointers to strings, which, of course, are in two different positions and will not be equal. In the second case, you compare two characters that are indicated at the beginning of the line, which are both "h" and, of course, equal.

0
source

By the way, the second can also be unequal . Being equal on most platforms is possible to optimize the compiler, because these two lines of hello need not be stored in the same static memory space.

0
source

Source: https://habr.com/ru/post/1390698/


All Articles