Search code examples
cstrcmp

strcmp returns __strcmp_sse2_unaligned () in a simple string search programme


I am currently fighting with a primitive search routine. It uses strcmp to compare a string given against a two dim array of strings. GDP returns:

"__strcmp_sse2_unaligned () at ../sysdeps/x86_64/multiarch/strcmp-sse2-unaligned.S:30 30 ../sysdeps/x86_64/multiarch/strcmp-sse2-unaligned.S: No such file or directory".

Edited: Trying to move on, added command line for string input procedure. Somehow, it is mistaken.

here is my code

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char dictionary()
{
   
    char **strings = (char**)malloc(5*sizeof(char*));
    int i = 0;

    for(i = 0; i < 5; i++){
        //printf("%d\n", i);
        strings[i] = (char*)malloc(7*sizeof(char));
    }

    sprintf(strings[0], "mark");
    sprintf(strings[1], "ala");
    sprintf(strings[2], "wojtek");
    sprintf(strings[3], "tom");
    sprintf(strings[4], "john");
   
    for(i = 0; i < 5; i++){
        printf("Line #%d(length: %lu): %s\n", i, strlen(strings[i]),strings[i]);
    } 

    for(i = 0; i < 5; i++){
        free(strings[i]);
    }

    free(strings);
}

int cmp(char *s1, char *s2[][10]){
    int i = 0;
    //size_t l = strlen(s1);
    
    for (i = 0; i < 5; i++){
            
            if (strcmp(s1, s2[i][7*sizeof(char)]) == 0)
            
            {
            
                printf("OK \n");
                            
                } else {
        
    printf("sth is wrong \n");  
            }
return 0;
    }
}

int main(){

    char BufText[255];
    int n=0;
    char sign;
    fflush(stdin);
    n = 0;
    do {
        sign = getchar();
        BufText[n ++] = sign;
        if(n >= 253) break;
    } while (sign !='\n');
    BufText [n] = 0;
    char **dict = dictionary();
    cmp(BufText, dict);
    free_dictionary(dict);
return 0;
}                                                                                                                                    

Solution

  • As said in the comments, there's a lot of flaws in your code.

    First in your main, you're trying to cmp("ala", dictionary); but dictionary is an undeclared variable. I think you wanted to use the result of your dictionary() call into the cmp call. So you need to store the dictionary() result into your dictionary variable. It can't actually be done because your dictionary() func does not return anything and free the allocated dict before it can be used.

    I could continue this way but here's a patched version of your code. Feel free to ask for clarifications.

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    char **dictionary()
    {
    
        char **dict = (char**)malloc(sizeof(char*) * 5);
        int i = 0;
    
        for (i = 0; i < 5; i++)
            dict[i] = (char*)malloc(sizeof(char) * 7);
    
        sprintf(dict[0], "mark");
        sprintf(dict[1], "ala");
        sprintf(dict[2], "wojtek");
        sprintf(dict[3], "tom");
        sprintf(dict[4], "john");
    
        for (i = 0; i < 5; i++)
            printf("Line #%d(length: %lu): %s\n", i, strlen(dict[i]),dict[i]);
    
        return (dict);
    }
    
    void free_dictionary(char **dict)
    {
        for (int i = 0; i < 5; i++)
            free(dict[i]);
        free(dict);
    }
    
    void cmp(char *s1, char *s2[5])
    {
        int i = 0;
    
        for (i = 0; i < 5; i++)
        {
            if (strcmp(s1, s2[i]) == 0)
                printf("OK \n");
            else
                printf("sth is wrong \n");
        }
    }
    
    int main()
    {
        char **dict = dictionary();
    
        cmp("ala", dict);
        free_dictionary(dict);
    
        return (0);
    }