title: Why does a messed up character print(C)?
body: <p>I'm writing a <code>strovr</code> function, which finds all the intersections of two strings. For some reason, it occasionally prints mangled characters when dealing with a 1 character long intersection.</p>
  
  <p>For example, with the strings <code>Hey Brother</code> and <code>Hey Bro</code>, it produces:
   </p>
  
  <pre class="lang-none prettyprint-override"><code>Bro
  He
  o
  rL?z?//MANGLED
  e
  </code></pre>
  
  <p>I assume it has to do with a pointer/memory issue.</p>
  
  <p>Here is my code.
  </p>
  
  <pre class="lang-c prettyprint-override"><code>#import &lt;stdio.h&gt;
  #import &lt;stdlib.h&gt;
  #import &lt;string.h&gt;
  
  char **strovr(char c1[], char cmp[], int *len_response)
  {
      int lenc1 = strlen(c1), lencmp = strlen(cmp);
      int len_big = lenc1 &gt; lencmp ? lenc1 : lencmp;
      int len_small = lenc1 &lt; lencmp ? lenc1 : lencmp;
      char **both[2] = {&amp;c1, &amp;cmp};
      char **returned = (char **)malloc(sizeof(char *));
      int size_returned = 0;
      int indlonger = lenc1 &gt; lencmp ? 0 : 1, indshorter = !indlonger, i = len_small;
      char A, B;
  
      while (i &gt;=(0-len_big))
      {
          int i_incr = i &gt;= 0 ? i : 0;
          int j      = i &gt;= 0 ? 0 : 0-i;
          int until  = i &gt;= 0 ? len_small : i +len_big;
          char A = (*both[indshorter])[i_incr];
          B = (*both[indlonger])[j];
  
          int currently_in = 0;
          int ind_begin;
  
          while (i_incr &lt;until)
          {
  
              if (B==A &amp;&amp; B!=32 &amp;&amp; A!=32)
              {
                  if (!currently_in)
                  {
                      ind_begin = j;
                      currently_in = 1;
                  }
  
                  if (((*both[indshorter])[i_incr+1] != (*both[indlonger])[j+1]) || (i_incr+1 == len_small) )
                  {
                      currently_in = 0;
                      int ind_end = j;
                      char *match = malloc(sizeof(char) * (ind_end-ind_begin));
                      if (match != NULL )   //DIAGNOSIS SHOULD START HERE.
                      {
                          int curr_ind = 0;
                          if (ind_end-ind_begin &gt; 0)
                          {
                              for (int c = ind_begin; c&lt;=ind_end; c++)
                              {
                                  match[curr_ind] = (*both[indlonger])[c];
                                  curr_ind++;
                              }
                              //                            printf("%s ",match);
                          }
                          else
                          {
                              match[0] = A;
                              //                            match[1] = "c";
                              printf("%s: STRLEN(%lu)\n",match,strlen(match));
                          }
  
                          if (size_returned == 0) {}
                          else returned = realloc(returned, sizeof(char*)*(size_returned+1));
  
                          if (match == NULL)
                          {
                              printf("\nError::Return string not properly initialized\n");
                              exit(1);
                          }
  
                          returned[size_returned] = match;
                          size_returned++;
                      }
                  }
  
              }
  
              i_incr++;
              j++;
              A = (*both[indshorter])[i_incr];
              B = (*both[indlonger])[j];
          }
          i--;
      }
      *len_response = size_returned;
      return returned;
  }
  
  
  int main()
  {
      int resp;
      char **intersections = strovr("Hey Brother", "Hello Bro",&amp;resp);
  
      printf("\nThe intersection of Hey Brother and Hello Bro is:\n");
      for(int i = 0; i &lt; resp; i++)
      {
          for (int j = 0; j &lt; strlen(intersections[i]); j++)
          {
              printf("%c",intersections[i][j]);
          }
          printf("\n");
      }
  }
  </code></pre>