Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linux, qsort, array, pointer problem -revised-

Status
Not open for further replies.

CNewbie

Programmer
Dec 20, 2001
18
0
0
US
Ok this code is just like the code before, but it shows the exact problem, it will compile fine, no errors, no warnings, but when it runs it just gets a "Segmentation Fault", and outputs nothing, but if I // remark out the qsort( funtion line, it compiles fine, and outputs perfectly, albeit unsorted.
Any help, ideas, again, im using linux and gcc.

gcc -g -Wall -I../../../include test.c -o test

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>

char tstring[256];
char tgot[256];
char tname[256];
char tdata[50000][256];
int tfi=0;
int i=0;
DIR *pd;
struct dirent *allp;

static int scomp(const void *p1, const void *p2)
{
char *a = * (char **) p1;
char *b = * (char **) p2;
return strcasecmp(a,b);
}

int main()
{
pd = opendir (&quot;/mnt/winc/Music&quot;);
if ( pd != NULL ) {
while ( (allp = readdir(pd)) ) {
sprintf(tstring,&quot;%s&quot;,allp->d_name);
strcpy(tgot,tstring);
strcpy(tname,(&tgot[0]));
sprintf(tdata[tfi],&quot;%s&quot;,tname);
tfi++;
}
}
tfi=tfi-1;
closedir(pd);
qsort(tdata, tfi, sizeof tdata[0], scomp);
i=0;
do {
printf(&quot;%s\n&quot;,tdata);
i++;
}while (i<tfi);
printf(&quot;%d\n&quot;,i);
return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top