You can't (usefully) compare strings using != or ==, you need to use strcmp:
while (strcmp(check,input) != 0)
The reason for this is because != and == will only compare the base addresses of those strings. Not the contents of the strings themselves.
You can't (usefully) compare strings using != or ==, you need to use strcmp:
while (strcmp(check,input) != 0)
The reason for this is because != and == will only compare the base addresses of those strings. Not the contents of the strings themselves.
Ok a few things: gets is unsafe and should be replaced with fgets(input, sizeof(input), stdin) so that you don't get a buffer overflow.
Next, to compare strings, you must use strcmp, where a return value of 0 indicates that the two strings match. Using the equality operators (ie. !=) compares the address of the two strings, as opposed to the individual chars inside them.
And also note that, while in this example it won't cause a problem, fgets stores the newline character, '\n' in the buffers also; gets() does not. If you compared the user input from fgets() to a string literal such as "abc" it would never match (unless the buffer was too small so that the '\n' wouldn't fit in it).
Fast String comparisons in C
simple comparison between std::string and c-string
This is a super noob question, but strcmp returns 0 if the strings are equal and "truthy" if they are not equal, like if(-1) is true? Is there a different way to compare strings or do I just need to wrap my head around this?
Why pointers to strings are equals betweent them, but array of chars are different between them
Videos
Hi, I'm working just for fun on a fast comparison for strings written in C, with the intent of being faster than the normal strncmp function, which is currently the code bellow
```
int fast_strncmp(const char *str1, const char *str2, int len) {
const char *final_pos = (str1 + len) - 4;
while (str1 < final_pos) {
// if characters differ, or end of the second string is reached
if (*((uint32_t *)str1) != *((uint32_t *)str2)) {
break;
}
// move to the block of characters
str1 += 4;
str2 += 4;
}
final_pos += 4;
while (str1 < final_pos) {
if (*str1 != *str2 || *str1 == 0 || *str2 == 0) {
return *str1 - *str2;
}
// move to the next pair of characters
str1++;
str2++;
}
return 0;
}
```Is there any clear problem with the code that could make it a bad option for fast string comparisons. When I wrote it a couple of weeks ago, I didn't think there could be any problem with it, but this week I was watching a couple of videos about C programming and it was mentioned that casting an array of 4 uint8_t to a uint32_t could be a problem. I'm even using this function at work and haven't had a single problem or warning, but since I'm planning to make a youtube video about it, I want to guarantee that my code won't be a problem for other people.
On top of that I've made a couple of benchmarks on the performance to be sure it really is fast, so I've compared it to strncmp and an implementation by https://github.com/mgronhol, that I found here: https://mgronhol.github.io/fast-strcmp/, which got me the following results:
EDIT: reddit was not cooperating with me posting the results text in a well formatted way, so here's the link to the file:
https://github.com/BenjamimKrug/fast_string_comparison/blob/main/results.txt
As you can see, running on the STM32 and the ESP32, my algorithm runs faster by a little compared to the fast_compare function by mgronhol, but running on my PC, it's performing terribly. Does anyone know why that is?
You can find more info about the code in my github repository where I put everything related to this test: https://github.com/BenjamimKrug/fast_string_comparison
P.S.: Sorry if this is the wrong subreddit for this kind of thing, I was going to post it on r/programming, but after reading the rules, I saw that maybe it was best to post it here.
EDIT: fixed code formatting
Hello,
I am discussing with a person who believes that
std::string msg = "Hello";
if (msg == "Hello") { // UB?
...
}According to the person, this line "if (msg == "Hello") {" is UB.
Have you seen this "operator==" as UB?
I have checked the C++ specification (from c++11), and it's acceptable. I checked the std::string implementation (GNU GCC 5 and the latest) and operator== calls std::string::compare and then "traits". Passing cstring in operator== is fine too...
Toolchain is ARM for 32bit architecture and std is c++11/14.
His arguments are based on Stackoverflow posts which are more than 10y ago...
Thank you
I am an experienced dev, but have no C experience and i'm currently working through reverse engineering one of the flipper zero apps and this really caught me off guard
I'm very sorry if this has been explained before I'm just trying to wrap my head around it