" (C) I am trying to figure out if there is a specific behavior for the following operation in C: char...">

Comparing strings with operators "<" or ">" (C)

I am trying to figure out if there is a specific behavior for the following operation in C:

 char *str1 = "some string"; char *str2 = "another string"; if (*str1 > *str2) //do something... else //do something else... 

(As mentioned in the title, I mean "comparison" between the lines using the ">" operator)

Obviously, if I tried:

 str1 > str2 

(without operator *)

it will calculate the results according to the pointer (i.e. string address) (on the right ...?). I was looking for answers to it, but could not find them, and I could not figure it out when playing with the code. thanks.

+5
source share
5 answers

With the indirection operator * you actually compare the values ​​of the characters pointed to by the pointers at the time of dereferencing.

So, in your code, it compares 's' with 'a' as follows 's' > 'a' , which is true.

Values ​​are of type char and, therefore, are well defined to use the operators < , > == , >= , <= != .

Be careful when declaring a pointer to a string literal, use the const qualifier to prevent it from accidentally changing, because it will be undefined.

+16
source

There is no "string type" in C, just the convention that a char * pointing to a char sequence terminated by a null byte is called "string".

To compare the contents of a string, use strcmp (const char * lhs, const char * rhs) .

*str1 > *str2 does not compare strings, compares their first characters.

str1 > str2 also does not compare strings, and although it "compares their addresses" on many platforms, strictly speaking, this behavior is undefined .

From C standard, 6.5.8 Relationship operators (including > ), clause 5, emphasize mine:

When comparing two pointers, the result depends on the relative locations in the address space of the objects that it points to. If two pointers to an object or incomplete types point to the same object or both point to one minus to the last element of the same array object , they compare equal ones.

If the objects pointed to are members of the same aggregate object, pointers to structure elements declared later are compared more than pointers to elements declared earlier in the structure, and pointers to array elements with large index values ​​are compared more than pointers to elements of the same array with lower index values.

All pointers to members of the same association are compared with equal ones. If the expression P points to an element of an array object, and the expression Q points to the last element of the same array object , the expression of the pointer Q + 1 is compared more than P.

In all other cases, the behavior is undefined.

+6
source

Without the * operator, you are actually comparing str1 and str2 addresses , which causes undefined behavior, since they are not pointers to the same aggregate object or union or object.

However, with the * operator, you search for your addresses, so the compared values ​​become the values ​​of the two elements of the first lines of the line. There is no undefined behavior in this code.

+4
source

"some string" and "another string" are string literals. They have types of character arrays char[12] and char[15] respectively char[15]

In these statements

 char *str1 = "some string"; char *str2 = "another string"; 

pointers str1 and str2 initialized with the addresses of the first characters of string literals.

You want to compare these string literals, which are two strings stored in character arrays.

If you write

 if (*str1 > *str2) //do something... 

then *str1 and *str2 are only the first characters of string literals. This condition in an if statement compares two characters. This is equivalent to the following statement

 if (str1[0] > str2[0]) //do something... 

Arrays do not have relational operators, such as <, <=,> or> =. If you want to compare two arrays, you must compare their elements separately.

However, for character arrays containing strings (a sequence of characters terminated by zero), there are standard functions such as strcmp , strncmp , memcmp declared in the <string.h> header

For example (C standard, 7.23.4.2 strcmp function)

3 The strcmp function returns an integer greater than, equal to, or less than zero, respectively, since the string pointed to by s1 is greater than, equal to, or less than the string pointed to by s2.

So, if you want to determine if the first string literal is larger than the second string literal, you must write

 if ( strcmp( str1, str2 ) > 0 ) //do something... 

If you want to determine if the first string literal is greater than or equal to the second string literal, you must write

 if ( strcmp( str1, str2 ) >= 0 ) //do something... 

If you want to determine if the first string literal is smaller than the second string literal, you must write

 if ( strcmp( str1, str2 ) < 0 ) //do something... 

If you want to determine if the first string literal is less than or equal to the second string literal, you must write

 if ( strcmp( str1, str2 ) <= 0 ) //do something... 
+1
source

You cannot compare two strings, such as two integers. You can use strcmp(char *firstStr, char *secondStr) . The return value of strcmp is an integer.

 char *str1 = "some string"; char *str2 = "another string"; int difference; difference = strcmp(str1,str2); if(difference < 0) { printf("str1 is less than str2"); } else if(difference > 0) { printf("str2 is less than str1"); } else if (!difference) //(difference == 0) { printf("str1 is equal to str2"); } 
+1
source

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


All Articles