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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linux, qsort, segmentation fault question.

Status
Not open for further replies.

CNewbie

Programmer
Dec 20, 2001
18
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;
}
 
I've never declared the comprison function as static, but I don't know if this is important here.
I can't see any error on your code, I'd try to put a printf inside scomp function to see if it's being executed at least once, and what the strings are inside it. I'm sorry I can't be more helpful :-(
BTW, why this
Code:
tfi=tfi-1;
line? You aren't sorting the last element. Oh, and I'd substitute sprintf calls by strcpy calls, it's safer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top