#include<stdio.h>
#include<stdlib.h>
#include<string.h>
static int compare(const void *x,const void *y){
    return strcmp(*(const char**)x, *(const char**)y);
}
int main(){
    FILE *p = fopen("file.txt","w");
    char ch = '\0',**c = (char**)calloc(6,sizeof(char*));
    int n[6]={0},i=0,j=0;
    fprintf(p,"jack\nakki\njohn\nrachael\nrobin\ntom");
    fclose(p);
    p = fopen("file.txt","r");
    while(1){
        while((ch=getc(p))!= '\n'){
            if(ch == EOF) break;
            putchar(ch);
            n[i]++;
        }
        printf(" %d\n",n[i]);
        if(ch == EOF) break;
        ch = '\0';
        i++;
    }
    ch = '\0';
    for(i=0;i<6;i++)
        c[i] = (char*) calloc(n[i],sizeof(char));
    fclose(p);
    i=0;
    p = fopen("file.txt","r");
    while(1){
        while((ch=getc(p))!= '\n'){
            if(ch == EOF) break;
            *(c[i]+j) = ch;
            j++;
        }
        i++;
        j = 0;
        if(ch == EOF) break;
        ch = '\0';
    }
    qsort(*c,6,sizeof(char*),compare);// why won't this work?
    for(i=0;i<6;i++)
        printf("%s\n",*c[i]);
    return 0;
}

Recommended Answers

All 3 Replies

So, what is your problem? Just posting code isn't usually helpful. In any case, the problem is passing *c to qsort instead of just c. The other problem is in your final print loop, you are passing *c[i] to the printf statement instead of c[i].

In any case, this is a situation where building a debuggable image and running it in the debugger would quickly show you what/where your problems were.

yeah I realized that minutes after posting this, sorry for the trouble and thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.