lfind() and strcmp()

This is a discussion on lfind() and strcmp() within the C Programming forums, part of the General Programming Boards category; Dear there, I got inconsistent result on using lfind() and strcmp(), can anyone help me out? Thanks. Code: #include <stdlib.h> ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    48

    lfind() and strcmp()

    Dear there, I got inconsistent result on using lfind() and strcmp(), can anyone help me out? Thanks.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <search.h>
    #include <string.h>
    
    int main(){
      char aName[250];
      aName[0]='O';aName[1]='H';aName[2]='\0';
      char **obj;
      obj = (char **)malloc(2*sizeof(char *));
      obj[0]= (char *)malloc(3*sizeof(char));
      obj[0][0]='O';
      obj[0][1]='H';
      obj[0][2]='\0';
    
    int result1= strcmp(aName, obj[0]);
     fprintf(stdout, "%d\n",result1);
     if (result1==0) fprintf(stdout, "aName equals obj[0]\n");
    
      void *result2;
      size_t nelp=1;
      result2 = lfind((const void*)aName, (const void *)obj, &nelp, sizeof(char *), (int (*) (const void*, const void*))strcmp);
    
      if (result2==NULL) 
        fprintf(stdout, "aName:%s not found in obj[]\n", aName);
      else 
        fprintf(stdout, "found\n");
    
      int i;
      fprintf(stdout, "obj[] is:\n");
      for (i=0;i<nelp;i++)
        fprintf(stdout, "%s\t", obj[i]);
      fprintf(stdout, "\n");  fprintf(stdout, "\n");
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you do
    Code:
    strcmp(aName, obj);
    you won't get 0. The value of obj and the value of obj[0] are not the same.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    48
    I think lfind() will call strcmp() with "aName" and one of obj[0], obj[1], ...int turn. However the result is not what I wanted. Any suggection?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by patiobarbecue View Post
    I think lfind() will call strcmp() with "aName" and one of obj[0], obj[1], ...int turn.
    You want that to happen. That has no real relation to the code you've written, however. Watch what happens during the calls:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <search.h>
    #include <string.h>
    
    int tester_compare(const void* left, const void* right) {
        printf("Compare is seeing %p and %p\n", left, right);
        return strcmp(left, right);
    }
    
    int main(){
        char aName[250];
        aName[0]='O';aName[1]='H';aName[2]='\0';
        char **obj;
        obj = (char **)malloc(2*sizeof(char *));
        obj[0]= (char *)malloc(3*sizeof(char));
        obj[0][0]='O';
        obj[0][1]='H';
        obj[0][2]='\0';
    
        int result1= tester_compare(aName, obj[0]);
        void *result2;
        size_t nelp=1;
        result2 = lfind((const void*)aName, (const void *)obj, &nelp, sizeof(char *), tester_compare);
    
        if (result2==NULL)
            fprintf(stdout, "aName:%s not found in obj[]\n", aName);
        else
            fprintf(stdout, "found\n");
    }
    lfind assumes the thing you are searching is a block of memory -- but a 2D malloc'd fake array is not such a thing.

Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21