title: C program to sort char arrays using a pointer array
body: <p>I am new to C and trying to code a program to sort char arrays by shorting an array of pointers pointing on them(A program as in D&amp;K The C programming language pg-108). here is my code-</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #define MAXLINES 5000
  #define MAXLEN 100
  char *line[MAXLINES];
  int readlines(char *line[], int t);
  void writelines(char *line[], int t);
  void qsort(char *line[], int lo, int m);
  int getline(char *, int);
  char *alloc(int);
  int partition(char *line[], int lo, int m);
  int main()
  {
      int nlines;   //no of input lines
      if(nlines=readlines(line,MAXLINES)&gt;=0)
      {
  
          qsort(line, 0, nlines-1);
          writelines(line, nlines);
          return 0;
      }
      else
      {
          printf("Error too many ");
          return 1;
  
      }
  
  
  
  }
  char store[10000];   //storage for alloc function
  char *p = store;
  char *alloc(int n)   //next free position for allocating to the input lines 
  {
      if (p + n &lt;= store + 10000)
      {
          p += n;
          return p - n;
      }
      else
          return NULL;
  }
  int readlines(char *le[], int maxlines) //read the input lines to be sorted
  {
      char *p;
      int i = 0;
      char line[MAXLEN];
      int len;
      while ((len=getline(line,MAXLEN)) &gt; 0)
      {
          if ((i &gt;= maxlines)||((p = alloc(len)) == NULL))
          {
              return -1;
          }
          else
          {
              line[len - 1] = '\0';
              le[i++] = p;
              strcpy(p, line);
          }
  
      }
      return i;
  
  }
  void writelines(char *line[], int t)  /*function to print lines after 
  {                                          sorting */
      for (int i = 0; i &lt;= t - 1; i++)
          printf("%s\n",line[i]);
  
  }
  int getline(char a[], int t)    //function to read a line
  {
      int i = 0;                 //to count no of input char  
      int c;
      while ((--t &gt; 0) &amp;&amp; (c = getchar()) != '\n'&amp;&amp;c != EOF)
          a[i++] = c;
          ;
      if (c== '\n')
          a[i++]='\n';
      a[i] = '\0';
      return i;
  
  }
  
  void qsort(char *line[], int lo, int m) /*function to quick sort the pointer
  {                                         array*/
      if (lo &gt;= m)
          return;
      int p = partition(line,lo,m);
      qsort(line,p+1,m);
      qsort(line,lo,p-1);
  
  
  }
  int partition(char *line[], int lo, int m)   /*function for partition in 
  {                                               quick sort */
      int index=0;
      char *t = line[m];
      for (int i = lo; i &lt;= m - 1; i++)
      {
          if (strcmp(line[i], t) &lt;= 0)
          {
              char *p = line[i];
              line[i] = line[index];
              line[index] = p;
              index++;
          }
      }
          char *p = line[m];
          line[m] = line[index];
          line[index] = p;
          return index;
  }
  </code></pre>
  
  <p>It consists of a number of functions and I have gave a tried to give a sort description of each.Even after spending several hours I was unable to figure out why it prints the first input line only.Please help to figure out what are the problems in this code.</p>