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, question.

Status
Not open for further replies.

CNewbie

Programmer
Dec 20, 2001
18
0
0
US
I am very new to C and C+, and I am trying to teach myself from VERY outdated text books, and I can't seem to figure this out.
No matter what I do, the code below will either not compile with errors, or compile and then seg-fault when it hits the qsort function.
I have tried all changing the symbols around, but without good C programing skills, I'm grasping at straws.

I just want this to compile and not seg fault.
If someone could look it over, and maybe tell me where and why its having a problem. Maybe a breif explaination of how to copy a pointer to a 2-d array, and vice-versa.
Oh yea, I use GCC and Linux. This example may be off a * or & here and there. I think.

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

char tstring[256];
char tgot[256];
char tname[256];
char *tdata[500][256];
int tfi=0;
int i=0;
DIR *pd;
FILE *pFile;
struct dirent *allp;
static int scomp(const void *, const void *);
int main(){
pd = opendir (&quot;/mp3s&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);
pFile = fopen (&quot;List.m3u&quot;,&quot;wt&quot;);
do {
fputs(tdata[ti],pFile);
puts(tdata);
i++;
}while (i<=tfi)
fclose (pFile);
return 0;
}


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

ANY help would be great.
 
I have not played with qsort for a couple years, but I seem to recall that it takes a pointer to a function that does the comparison. It looks like scomp is your comparison function, but I don't see where you declare and initilaize a pointer to it. I think it needs something like:
char *comparison
comparison=&scomp

Ofcourse I'm so rusty with the syntax that I'm probably embarrassing myself!

<< If I don't pay my property taxes, they'll take my property. If I don't pay my syntax, will they take away my sins? >>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top