title: strcpy in C does not copy properly to overwrite a string
body: <p>I am trying to implement a infix to postfix conversion program in C. I wrote (cleanExpression) the following function to remove unwanted space in the given infix string expression. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdbool.h&gt;
  #include &lt;assert.h&gt;
  #include &lt;string.h&gt;
  
  #define EXPR_LIMIT 1000
  
  bool isNumber(char c) {
      return (c &gt;= '0' &amp;&amp; c &lt;= '9');
  };
  
  bool cleanInfixExpression(char *expr, char **clean) {
      *clean = malloc(sizeof(char) * strlen(expr) + 1);
      strcpy(*clean, expr);
      char *curr = *clean;
      char *temp;
      char *temp_dest;
      // first count the number of terms (numbers and operators).
      while (*curr != 0) {
          // check if the current position is space.
          if (*curr == ' ') {
              printf("Current:%s\n", curr);
              printf("Clean  :%s\n\n", *clean);
  
              // go through all space positions.
              for(temp = curr; *temp == ' '; temp++) {};
  
              // make its numbers on both side of the space.
              if (curr != *clean &amp;&amp; (isNumber(*(curr-1)) &amp;&amp; (isNumber(*temp)))) {
                  printf("\"%s\" : Invalid expression!\n", expr);
                  free(*clean);
                  return (false);
              }
  
              // copy into the the original string.
              strcpy(curr, temp);
          }
          curr++;
      }
      printf("Current:%s\n", curr);
      printf("Clean  :%s\n\n", *clean);
      return (true);
  };
  
  
  void main(void) {
      char expr[EXPR_LIMIT] = " 214      +              9523        - 235235";
      printf("INFIX expression: %s\n", expr);
      char *clean;
      if (cleanInfixExpression(expr, &amp;clean)) {
          printf("%s\n", clean);
      }
      return;
  }
  </code></pre>
  
  <p>When I run this code the following is the output I get.</p>
  
  <pre><code>INFIX expression:  214      +            9523        - 235235
  Current: 214      +            9523        - 235235
  Clean  : 214      +            9523        - 235235
  
  Current:      +            9523        - 235235
  Clean  :214      +            9523        - 235235
  
  Current:      952   9523        - 235235
  Clean  :214+      952   9523        - 235235
  
  Current:   9523        - 235235
  Clean  :214+952   9523        - 235235
  
  " 214      +            9523        - 235235" : Invalid expression!
  </code></pre>
  
  <p>However, if I change the strcpy(curr, temp) to the following code, where I manually copy, then I get the correct output.</p>
  
  <pre><code>temp_dest = curr;
  while(*temp != 0) {
      *(temp_dest++) = *(temp++);
  }
  *temp_dest = 0;
  </code></pre>
  
  <p>This the output with me manually copying the string, instead of using the strcpy function.</p>
  
  <pre><code>INFIX expression:  214      +              9523        - 235235
  Current: 214      +              9523        - 235235
  Clean  : 214      +              9523        - 235235
  
  Current:      +              9523        - 235235
  Clean  :214      +              9523        - 235235
  
  Current:              9523        - 235235
  Clean  :214+              9523        - 235235
  
  Current:        - 235235
  Clean  :214+9523        - 235235
  
  Current: 235235
  Clean  :214+9523- 235235
  
  Current:
  Clean  :214+9523-235235
  
  214+9523-235235
  </code></pre>
  
  <p>I am not able to understand what mistake I am making in using strcpy. Can someone tell me what might be going on?</p>