title: Dynamic Memory Allocation Using Temporary Array
body: <p>I'm having trouble dynamically allocating memory for an array. 
  The program is simply supposed to exchange the swap the first row with the second, and the third with the forth. I am getting strange results like:</p>
  
  <p>Enter string: hello</p>
  
  <p>Enter string: how are you</p>
  
  <p>Enter string: i'm good thanks</p>
  
  <p>Enter string: bye</p>
  
  <p>Enter string: bai</p>
  
  <p>Enter string: xx</p>
  
  <p>=========================</p>
  
  <p>how are you</p>
  
  <p>!i'm good thanks</p>
  
  <p>hello</p>
  
  <p>!how are you</p>
  
  <p>bye</p>
  
  <p>!bai</p>
  
  <p>i'm good thanks</p>
  
  <p>!bye</p>
  
  <p>bai</p>
  
  <p>!xx</p>
  
  <pre><code>    int count = 0;
      char *lines[MAX_LINES];
      char *tmp[50]; 
  
  
      printf("Enter string: ");
      fgets(tmp, 50, stdin);
      lines[count] = (char *) malloc((strlen(tmp)+1) * sizeof(char));
      strcpy(lines[count], tmp); 
  
      while(strcmp("xx\n", lines[count])){
              count++;
              printf("Enter string: ");
              fgets(tmp, 50, stdin); 
              lines[count] = (char *) malloc((strlen(tmp)+1)* sizeof(char));
  
              strcpy(lines[count], tmp); 
      }
  
  void exchange(char * records[])
  {
      char * temp;
      temp = records[0];
      records[0] = records[1];
      records[1] = temp;
  
      temp = records[2];
      records[2] = records[3];
      records[3] = temp; 
  
  }
  
  void printArray(char * inputs[], int row, int col)
  {
      int i, j;
  
      for(i = 0; i &lt; row; i++){
          for(j = 0; j &lt; col; j++){
              printf("%c", inputs[i][j]);
          }
  
      }
  }   
  </code></pre>