with a few small modifications it runs fine
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
long max( long a, long b ) {
return a > b ? a :b;
}
int cmp ( const void *a, const void *b )
{
return strcmp ( (const char *)a, (const char *)b );
}
int main(void) {
FILE *fp;
char **TwoDimenSortable, **prow, buff[10];
unsigned u, ulines, umaxlen;
if((fp=fopen("test.dat","r"))==NULL) {puts("fopen poop");exit(55);}
/* count input file lines and max width too */
for (ulines=0, umaxlen = 0; fgets(buff, sizeof buff,fp); ++ulines) {
umaxlen=max(umaxlen, strlen(buff)); /* get max width while we're at it */
}
rewind(fp);
umaxlen++;
/* malloc the mass 2 dimensional array */
if (NULL==(TwoDimenSortable=(char **)malloc (ulines * umaxlen * sizeof (char)))) {
printf("Jeez, couldn't malloc %u lines for %u cols.\n",ulines,umaxlen);
exit(70);
}
/* malloc pointers to each line */
if (NULL==(prow=malloc(ulines * sizeof(char *)))) {
puts("malloc failure - cheez! (tiny one)\n");
exit(44);
}
for (u=0; u<ulines; ++u) {
prow[u] = (char *) TwoDimenSortable + (u * umaxlen); /* qsortable pointer array */
fgets(buff, sizeof buff, fp);
if(buff[strlen(buff)-1]=='\n')buff[strlen(buff)-1]='\0'; /* stripEOL */
strcpy((char *)TwoDimenSortable+u*umaxlen, buff);
}
fclose(fp);
qsort(*prow, ulines, umaxlen, cmp);
for (u=0; u<ulines; ++u) {
//prow[umaxlen-1]='\0';
puts(prow[u]);
}
}