Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
The price of that beauty is a segfault if you somehow have lost the zero termination. strncmp to the rescue.
But the greatness of these functions is that C programmers actually know the performance issues of that stuff, even more with repeated strcat. Thats why C programs are often so fast. -
th4t6566yThis magic basically happens every time every kind of language get compiled or interpreted, reproducing those steps like C do when a string should be compared to another
-
@simulate except if string2 is shorter than string1 you access a pointer that isn't allocated, by doing that - segfault.
-
@gnulinuxer4fun it's still checked for. You don't access anything but return non-zero.
-
@gnulinuxer4fun on line 23, string1 could be replaced with string2 and the code would perform the same.
-
deodexed5736y@gnulinuxer4fun no, if string2 is smaller (meaning that you reach \0 before string1 does) it exits the loop and return non-zero, segfault in this case is impossible if both string have \0, it segfaults if you follow what @Fast-Nop said.
-
Yes, C can have some beauty to it... But C-strings are just an invitation to fuck up.
Related Rants
Almost forgot how beautiful C is:
3 statements tell you if two strings are similar.
rant
c