C qsort不对结构数组进行排序

C qsort不对结构数组进行排序

问题描述:

我有一个程序设计为读取单词并将其拆分,分开并分别计数(标点符号不同的单词被故意计为不同的单词).

I have a program that's designed to read in words and split them apart, separating each and counting them up individually (words with punctuation differences are counted as different words on purpose).

typedef struct word
{
    char letters[100];
    int count;
} Word;

int compare (const void *a, const void *b)
{
    return strcmp((*(struct word **) a)->letters,(*(struct word **) b)->letters);
}

int main()
{
    int sizeCheck = 0;
    Word ** collection = malloc(sizeof(Word*));
    Word * array = malloc(sizeof(Word));

    FILE *fptr, *fout;
    char fileName[80];
    char fileNameWords[80];
    char wordLine[100];
    strcpy(fileName,"data");
    strcpy(fileNameWords,fileName);
    strcat(fileNameWords,"data.out.txt");

操作开始的地方,假设打开文件一切正常(为方便起见,将其删除):

Where the action starts, assuming everything's fine with opening files (removed for shortness sake):

            int wordExists = 0;
            int t1 = 0;
            char tw1[100];


            fscanf(fptr,"%s",wordLine);
            strcpy(array->letters,wordLine);
            array->count = 1;
            collection[sizeCheck] = array;
            sizeCheck++;

            while (!feof(fptr))
            {
                wordExists = 0;
                fscanf(fptr,"%s",wordLine);
                for (t1 = 0; (t1 < sizeCheck) && (wordExists == 0); t1++)
                {
                    strcpy(tw1,array[t1].letters);
                    if (strcmp(tw1,wordLine) == 0)
                    {
                        array[t1].count += 1;
                        wordExists = 1;
                    }
                }
                if (!wordExists)
                {
                    collection = realloc(collection,(sizeCheck+1)*sizeof(Word*));
                    array = realloc(array,(sizeCheck+1)*sizeof(Word));
                    strcpy(array[sizeCheck].letters,wordLine);
                    array[sizeCheck].count = 1;
                    collection[sizeCheck] = array;
                    sizeCheck++;
                }
            }

            qsort(collection,sizeCheck,sizeof(Word*),compare);

            for (t1 = 0; t1 < sizeCheck; t1++)
            {
                fprintf(fout,"%s - %d\n",array[t1].letters,array[t1].count);
            }
            free(collection);
        }
    }
    fclose(fptr);
    fclose(fout);
    return 0;
}

除使用qsort函数或底部附近的fprintf部分外,它大部分都使用指针对指针方法.在这一点上,我有些困惑.我在做什么错,这阻止了它输出已排序的文件?(按字母顺序对单词进行排序)

Using a pointer-to-pointer method, it works for the most part, except when it comes to either the qsort function, or the fprintf parts near the bottom. I'm a little stumped at this point. What am I doing wrong here that's preventing this from outputting a sorted file? (Sorting by alphabetical order with words)

仅对 collection 数组(指针数组)进行排序.它们指向的值( array 的元素)保持不变.由于您 fprintf array 的元素,因此不会看到任何更改.

Only the collection array (an array of pointers) is being sorted. The values they point to (the elements of array) are unchanged. Since you fprintf the elements of array, you won't see any changes.

如果要对 array 进行排序,则可以使用 qsort 进行:

If you want to sort array, you can do that with qsort:

qsort(array, sizeCheck, sizeof(Word), compareWord);

其中 compareWord

int compareWord(const void *a, const void *b) {
    const Word *wa = a;
    const Word *wb = b;
    return strcmp(a->letters, b->letters);
}

或者,仅打印出 collection 中的元素,而不是 array :

Alternately, just print out the elements from collection instead of array:

fprintf(fout, "%s - %d\n", collection[t1]->letters, collection[t1]->count);