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

Pointer - Array Question/Problem -Revised-

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 dont think you are sorting how you mean to.

tdata, from what I can assume, will hold up to 50000 words of max length of 256.

I dont know if this is correct but with a small modification we could try

Code:
char* tdata[50000];
memset(tdata,0,sizeof(tdata));

//this would require a malloc of strlen(tname)+1 for each
//element
while ( (allp = readdir(pd)) ) 
{
      sprintf(tstring,&quot;%s&quot;,allp->d_name);
      strcpy(tgot,tstring);
      strcpy(tname,(&tgot[0]));
      tdata[tfi] = (char*)malloc(strlen(tname)+1);
      sprintf(tdata[tfi],&quot;%s&quot;,tname);
      tfi++;
}

// now try qsort call with
qsort(tdata,tfi,sizeof(char*),scomp);


I have not tested this but I think it should work for you. Please post if if you need more help

Matt


 
I changed it to:

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

char tstring[256];
char tgot[256];
char tname[256];
char* tdata[50000];

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()
{

memset(tdata,0,sizeof(tdata));
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]));
tdata[tfi] = (char*)malloc(strlen(tname)+1);
sprintf(tdata[tfi],&quot;%s&quot;,tname);
tfi++;
}
}
tfi=tfi-1;
closedir(pd);
qsort(tdata,tfi,sizeof(char*),scomp);
i=0;
do {
printf(&quot;%s\n&quot;,tdata);
i++;
}while (i<tfi);
printf(&quot;%d of %d\n&quot;,i,tfi);
return 0;
}


and it worked!!!, thank you so much Zyrenthian, i dont understand why it looks like the array is not 2d, is there a place i can go to read up on this type of thing,

none the less, thank you.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top