title: Binary search, strcmp two dynamic arrays of strings in C
body: <p>I'm fairly new to C programming but trying my best to understand it. I have two dynamic strings that are populated from two plain text files. One being a form of a dictionary, and the other one just a user input. What I want to get is binary search each user input word in the dictionary and find out if it is present (sort of a spell checker I guess).</p>
  
  <p>I'm stuck on my binary search function:</p>
  
  <pre><code>char **dictElem;
  int dictSize;
  char **inputElem;
  
  int binsearch(const char *val){
    int pos;
    int beg=0;
    int end=dictSize-1;
    int cond=0;
  
    while (beg&lt;=end){
      pos=(beg+end)/2; //Jump in the middle
      if ((cond=strcmp(dictElem[pos],val)) == 0)
        return pos;
      else if (cond&lt;0)
        beg=pos+1;
      else
        end=pos-1;
    }
    return 0;
  }
  </code></pre>
  
  <p>Both <code>dictElem</code> and <code>inputElem</code> were already read by other methods and (let's say) both [0] elements are equal strings <code>"aa"</code>.</p>
  
  <p>However after I run the <code>binsearch(inputElem[0]</code> it always returns 0. I tried just <code>strcmp(dictElem[0],inputElem[0])</code> and it returns 1.</p>
  
  <p>Where am I going wrong? Is it comparing char** and char*?</p>
  
  <p><strong>UPD:</strong>
  Function that's loading the <code>dictElem</code></p>
  
  <pre><code>void readd(FILE *file){
    int i=0,size=0; /* local size */
    char line[1024]; /* Local array for a single word read */
    printf("Loadingn dict...\n");
    while ((fgets(line,sizeof(line),file))!=NULL){
      dictElem=(char**)realloc(dictElem,(size+1)*sizeof(char *));
      dictElem[size++]=strdup(line);
    }
    printf("Total elements loaded: %d\n",size);
  }
  </code></pre>
  
  <p>Function that reads a user file is very similar, just a little different format.</p>