title: Sorting argv Inserts Random Numbers
body: <p>I am trying to sort *argv[] and I am getting random numbers printing out when i try and display the results of sorting. </p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  void sortIntegers(int value[], int length);
  int convertToInt(char *string);
  int *sortArguments(int argc, char *argv[]);
  
  int main() {
  
      int i;
      int value[9] = {0, 234, 345345, 91, -3, 12, 3, 19, 17};
      char *argv[9] = {"0", "234", "345345", "91", "-3", "12", "3", "19", "17"};
      int argc = 9;
      int *result;
  
      printf("\n");
  
  /* order before sorting */
      printf("Before: ");
      for(i = 0; i &lt; 9; i++){
          printf("%d ", value[i]);
      } 
  /* end or sorting proof */
  
  /* converting char sting to integers and sorting */
      result =sortArguments(argc, argv);
      printf("\nAfter: ");
      for (i = 0; i &lt; argc; i++) {
          printf("%d, ", result[i]);
      }
      printf("\n");
  /* end of sorting and conversion */
  
  /* this is to prove my sort algorith works */
      sortIntegers(value, 9);
      printf("Desired Results: ");
      for(i = 0; i &lt; 9; i++){
          printf("%d ", value[i]);
      }
      printf("\n\n");
  /* end or sorting proof */
  
      free(result);
      return 0;
  }
  
   void sortIntegers(int value[], int length) {     
  
      int i;
      int x;
      int temp;
  
      for(x = 0; x &lt; (length - 1); x++)
      {
          for(i = 0; i &lt; (length - x -1); i++)
          {
              if(value[i] &gt; value[i+1])
              {
                  temp = value[i];
                  value[i] = value[i + 1];
                  value[i + 1] = temp;
              }
          }
      }
  }
  int convertToInt(const char *string) {
      char stringToNum;
  
      stringToNum = atoi(string);
  
      return stringToNum;
  }
  int *sortArguments(int argc, char *argv[]) {
  
      int *list = malloc(sizeof(int) * (argc));
      int i;    
  
      for (i = 0; i &lt; argc; i++) 
      {    
          list[i] = convertToInt(argv[i]);
      }
      sortIntegers(list, (argc));
  
      return list;
  }
  </code></pre>
  
  <p>I have viewed the question <a href="https://stackoverflow.com/questions/37105083/bubble-sorting-random-numbers">Bubble sorting random numbers</a> and I believe i have not committed this error. </p>
  
  <p>here is the output of my program:
  <code>Before: 0 234 345345 91 -3 12 3 19 17 
  After: -22, -3, 0, 1, 3, 12, 17, 19, 
  Desired Results: -3 0 3 12 17 19 91 234 345345</code></p>
  
  <p>The before statement comes from a sorting test within my main, that is done correctly, because of this i believe my problem is either with pointer work or calling the function. </p>
  
  <p>does anyone know why sorting would insert random numbers?</p>