title: In C, sort array of strings by string length
body: <p>So I input strings into an array <code>mydata[10][81]</code></p>
  
  <pre><code>while ((ct&lt;=10) &amp;&amp; gets(mydata[ct]) != NULL &amp;&amp; (mydata[ct++][0] != '\0'))
  </code></pre>
  
  <p>I then use a for loop to create a second array of pointers </p>
  
  <pre><code>for (i=0;i&lt;11;i++){
      ptstr[i] = mydata[i];
  }
  </code></pre>
  
  <p>This is where I get stuck
  I know I need to use <code>strlen</code> somehow, but I can't even conceive of how to get the length of a pointer and then re-assign that pointer a new position based on a third additional value of length</p>
  
  <p>Hopefully that makes sense, I'm so lost on how to do it or explain it, I'm just trying to sort strings by length using array positions (not using something like <code>qsort</code>)</p>
  
  <p>I did some more work on it and came up with this:
  any idea why its not working?</p>
  
  <pre><code>void orderLength(char *ptstr[], int num){
  int temp;
  char *tempptr;
  int lengthArray[10];
  int length = num;
  int step, i, j, u;
  for (i=0; i&lt;num;i++){
      lengthArray[i] = strlen(ptstr[i]);
  }
  for (step=0; step &lt; length; step++){
      for(j = step+1; j &lt; step; j++){
            if (lengthArray[j] &lt; lengthArray[step]){
                temp = lengthArray[j];
                lengthArray[j] = lengthArray[step];
                lengthArray[step] =temp;
                tempptr=ptstr[j];
                ptstr[j]=ptstr[step];
  
                }
            }
      }
      for (u=0; u&lt;num; u++){
          printf("%s \n", ptstr[u]);
          }    
  } 
  </code></pre>