title: After using realloc the next pointer in array is lost
body: <pre><code>void operation2(char **p, int n, char *sir) {
      int i, move, k, xlen, ylen;
      char *x, *y, *q, separatori[] = " \'\",!?";
      x = strtok(sir, " ");
      y = strtok(NULL, " ");
      xlen = strlen(x);
      ylen = strlen(y);
      move = ylen - xlen;
      for (i = 0; i &lt; n; i++) {
          k = 0;
          while (strstr(p[i] + k, x)) {
              q = strstr(p[i] + k, x);
              if ((strchr(separatori, *(q - 1)) || q == p[i]) &amp;&amp;
                  (*(q + xlen) == '\0' || strchr(separatori, *(q + xlen)))) {
                  if (move &gt; 0 &amp;&amp; k == 0)
                      p[i] = realloc(p[i], (strlen(p[i]) + move * counter(p[i], x) + 1) * sizeof(char));
                  q = strstr(p[i] + k, x);
                  memmove(q + xlen + move, q + xlen, strlen(q + xlen) + 1);
                  memcpy(q, y, ylen);
                  k = strlen(p[i]) - strlen(q) + ylen;
                  if (move &lt; 0)
                      p[i] = realloc(p[i], (strlen(p[i]) + move + 1) * sizeof(char));
              } else
                  k = k + xlen;
          }
          puts(p[i]);
      }
  }
  </code></pre>
  
  <p>The code aims to find and replace a word (<code>x</code>) with a second one (<code>y</code>) in a text dynamically allocated in <code>**p</code>. They come in a string (<code>sir</code>) and are separated.
  <code>move</code> stores the difference between the words that are obtained. <code>n</code> represents the number of lines in the text.</p>
  
  <p>The word <code>x</code> must not be within another word hence the need to check for the separators.</p>
  
  <p>If the conditions are met then the string is reallocated depending if the <code>move</code> is positive or negative. If it's positive the string will be longer and can be reallocated for all the apparitions of the word <code>x</code> inside of it. <code>counter</code> is a function that counts the apparitions in the string.
  When <code>move</code> is negative the string must be diminished so the reallocation is done after the operations take place.</p>
  
  <p>The replace is done with <code>memmove</code> and <code>memcpy</code>.</p>
  
  <p><code>k</code> is the position after the apparitions of <code>x</code>.</p>
  
  <p>During a test there was the need to replace <code>"o"</code> with <code>"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"</code>. </p>
  
  <p>This is the reference 
  <a href="http://i.stack.imgur.com/lD4AX.jpg" rel="nofollow">Reference</a></p>
  
  <p>And this is what I get
  <a href="http://i.stack.imgur.com/yiXzi.jpg" rel="nofollow">Result</a></p>
  
  <p>When replacing <code>"o"</code> in the middle of the string an error occurs and the pointer to the next line is lost, pointing to the ending part of the previous line. The 1 represents the value of the counter for the line that follows </p>
  
  <blockquote>
    <p>Does <code>realloc</code> use memory that was already allocated and by doing so the next pointer is lost?</p>
  </blockquote>
  
  <p><strong>EDIT:</strong> Here is the allocation of the array:</p>
  
  <pre><code>int n, i;
  scanf("%d", &amp;n);
  char **p, *aux;
  p = malloc(n * sizeof(char *));
  aux = malloc(12000 * sizeof(char));
  getchar();
  for (i = 0; i &lt; n; i++) {
      fgets(aux, 12000, stdin);
      p[i] = malloc((strlen(aux) + 1) * sizeof(char));
      strcpy(p[i], aux);
      p[i][strlen(p[i]) - 1] = '\0';
  }
  free(aux);
  </code></pre>