strcmp

Why is strcmp so much faster than my function?

☆樱花仙子☆ 提交于 2019-12-21 03:21:09
问题 I wrote a function, Str::Compare , that is basically a strcmp rewritten in another way. While comparing the two function, in a loop repeted 500'000'000 times, strcmp execute too much fast, about x750 times faster. This code was compiled in a C library with -Os parameter active: int Str::Compare(char* String_1, char* String_2) { char TempChar_1, TempChar_2; do { TempChar_1 = *String_1++; TempChar_2 = *String_2++; } while(TempChar_1 && TempChar_1 == TempChar_2); return TempChar_1 - TempChar_2;

Why is strcmp so much faster than my function?

无人久伴 提交于 2019-12-21 03:21:03
问题 I wrote a function, Str::Compare , that is basically a strcmp rewritten in another way. While comparing the two function, in a loop repeted 500'000'000 times, strcmp execute too much fast, about x750 times faster. This code was compiled in a C library with -Os parameter active: int Str::Compare(char* String_1, char* String_2) { char TempChar_1, TempChar_2; do { TempChar_1 = *String_1++; TempChar_2 = *String_2++; } while(TempChar_1 && TempChar_1 == TempChar_2); return TempChar_1 - TempChar_2;

strcmp behaviour

本秂侑毒 提交于 2019-12-20 12:36:30
问题 When I run the following code: #include <stdio.h> int main(int argc, char *argv[]) { int p = 0; p = strcmp(NULL,"foo"); return 0; } I get segmentation fault. echo $? says 139. But when I run #include <stdio.h> int main(int argc, char *argv[]) { int p = 0; strcmp(NULL,"foo"); // Note removed assignment return 0; } I don't get any segmentation fault. Could someone please throw some light? Here is my gcc info: > gcc --version gcc (GCC) 3.4.6 20060404 (Red Hat 3.4.6-8) 回答1: You are probably using

SIGSEV on strcmp of memset string

99封情书 提交于 2019-12-20 07:49:17
问题 In the following program, i am expecting the for loop to stop after 3 elements. But it keeps on going indefinitely and fails later on with a coredump. is malloc() needed for char*[]` would strcmp fail if i memset to 0? . #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { char* str[10]; memset(str,0,10); str[0]=malloc(sizeof(char)*(strlen("Sample")+1)); str[1]=malloc(sizeof(char)*(strlen("Sample")+1)); str[2]=malloc(sizeof(char)*(strlen("Sample")+1)); strcpy(str[0],"Sample");

strcmp doesnt give 0, but the two args are the same

会有一股神秘感。 提交于 2019-12-20 05:47:08
问题 I can't see what I'm doing wrong. void contains(NODE *head, char *user) { NODE *this = head; while(this != NULL) { strcpy(temp, this->data); // printf("%s\n",user); // printf("%s\n",temp); printf("%d test\n", strlen(user)); printf("%d node\n", strlen(temp));; printf("%d\n",strcmp(temp, user)); if(strcmp(this->data, user) == 0){ printf("Found data %s\n", this->data); } this = this->next; } } And I have this in main (contains is called on the last line): NODE *head; head = malloc(sizeof(NODE));

scanf and strcmp with c string

烂漫一生 提交于 2019-12-20 04:04:10
问题 I found a nice example of how to use strcmp, but it's only working with fgets(), and i need to make it work with scanf. So, here's the code: int main(void) { char fruit[] = "apple\n"; char ans[80]; do { printf ("Guess my favorite fruit? "); scanf ("%s",ans); } while (strcmp (fruit, ans) != 0); puts ("Correct answer!"); return 0; } Even when I write the correct answear ("apple") it stays in the loop and keeps asking me what is the favorite fruit... I'm guessing it has something to do with the

Creating my own strcmp () function in C

自闭症网瘾萝莉.ら 提交于 2019-12-19 11:35:39
问题 I was assigned by my teacher to write my own strcmp() function in C. I did create my own version of said function, and I was hoping to get some feedback. int CompareTwoStrings ( char *StringOne, char *StringTwo ) { // Evaluates if both strings have the same length. if ( strlen ( StringOne ) != strlen ( StringTwo ) ) { // Given that the strings have an unequal length, it compares between both // lengths. if ( strlen ( StringOne ) < strlen ( StringTwo ) ) { return ( StringOneIsLesser ); } if (

Finding unique elements in an string array in C

僤鯓⒐⒋嵵緔 提交于 2019-12-19 03:39:19
问题 C bothers me with its handling of strings. I have a pseudocode like this in my mind: char *data[20]; char *tmp; int i,j; for(i=0;i<20;i++) { tmp = data[i]; for(j=1;j<20;j++) { if(strcmp(tmp,data[j])) //then except the uniqueness, store them in elsewhere } } But when i coded this the results were bad.(I handled all the memory stuff,little things etc.) The problem is in the second loop obviously :D. But i cannot think any solution. How do i find unique strings in an array. Example input : abc

strcmp behaviour in 32-bit and 64-bit systems

我只是一个虾纸丫 提交于 2019-12-17 21:32:59
问题 The following piece of code behaves differently in 32-bit and 64-bit operating systems. char *cat = "v,a"; if (strcmp(cat, ",") == 1) ... The above condition is true in 32-bit but false in 64-bit. I wonder why this is different? Both 32-bit and 64-bit OS are Linux (Fedora). 回答1: The strcmp() function is only defined to return a negative value if argument 1 precedes argument 2, zero if they're identical, or a positive value if argument 1 follows argument 2. There is no guarantee of any sort

Inconsistent strcmp() return value when passing strings as pointers or as literals

£可爱£侵袭症+ 提交于 2019-12-17 16:15:11
问题 I was playing around with strcmp when I noticed this, here is the code: #include <string.h> #include <stdio.h> int main(){ //passing strings directly printf("%d\n", strcmp("ahmad", "fatema")); //passing strings as pointers char *a= "ahmad"; char *b= "fatema"; printf("%d\n",strcmp(a,b)); return 0; } the output is: -1 -5 shouldn't strcmp work the same? Why is it that I am given different value when I pass strings as "ahmad" or as char* a = "ahmad" . When you pass values to a function they are