2012-11-28 52 views
1

我正在尝试使用C提供的内置快速排序来排序指向结构的指针数组。我想根据结构中的名称元素对每个元素进行排序。将指针数组快速排列到结构中

尽管我每次通过比较函数调试整个数组的输出时,都表明该函数的确在切换元素,但最终的结果并不是正确的排序顺序。有什么我只是没有看到这里?

typedef struct // The custom data type. 
{ 
    char *name; 
} Person; 

---------------------------- 

Person **people; // A dynamically allocated array of Person pointers. 
int numPeople; // The logical index of people. 
int maxPeople; // The current maximum capacity of people. 

int compare(const void *a, const void *b) // The comparison function for determining 
{           // alphabetic ordering. 
    const Person *const *p1 = a; 
    const Person *const *p2 = b; 
    return strcmp((*p1)->name, (*p2)->name); // Compare alphabetically, return result. 
} 

void SomeFunction(void) 
{ 
    qsort(people, numPeople, sizeof(Person *), compare); // Perform the sort. 
} 

感谢您的帮助。

+1

只是为了确认qsort正在找到正确的数据:您可以在'compare'中添加一个'printf'行来打印每个人的姓名。如果结果不符合预期,则可能'* a'和'* b'毕竟不是指向Person对象的指针。 – Edmund

+0

我只是这样做了,事实上它打印出了与结构相对应的名字,所以我猜这个部分是正确的。感谢您的建议。 –

+0

经过测试,在这里可以正常工作。 –

回答

1

尝试我已经测试你的代码,它看起来工作正常。这里是我用gcc 4.5.2编译的代码:

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

typedef struct // The custom data type. 
{ 
     char *name; 
} Person; 

Person **people; // A dynamically allocated array of Person pointers. 
int numPeople; // The logical index of people. 
int maxPeople; // The current maximum capacity of people. 

int compare(const void *a, const void *b) // The comparison function for determining 
{           // alphabetic ordering. 
     const Person *const *p1 = a; 
     const Person *const *p2 = b; 
     return strcmp((*p1)->name, (*p2)->name); // Compare alphabetically, return result. 
} 

void SomeFunction(void) 
{ 
     qsort(people, numPeople, sizeof(Person *), compare); // Perform the sort. 
} 

int main() 
{ 
     int iCnt; 

     maxPeople = 4; 
     numPeople = 4; 

     people = calloc(1, sizeof(Person *) * maxPeople); 

     people[0] = calloc(1, sizeof(Person)); 
     people[1] = calloc(1, sizeof(Person)); 
     people[2] = calloc(1, sizeof(Person)); 
     people[3] = calloc(1, sizeof(Person)); 

     people[0]->name = strdup("Tanya"); 
     people[1]->name = strdup("Alfred"); 
     people[2]->name = strdup("Harry"); 
     people[3]->name = strdup("Oakley"); 

     for(iCnt = 0; iCnt < numPeople; iCnt ++) 
       printf("[%d] %s\n", iCnt, people[iCnt]->name); 

     SomeFunction(); 

     for(iCnt = 0; iCnt < numPeople; iCnt ++) 
       printf("[%d] %s\n", iCnt, people[iCnt]->name); 

     return 0; 
} 

该代码看起来是合法的,我不知道什么是错的。你可以尝试编译我测试过的代码并查看它是否有效?

+0

事实证明,我在Person被输入的文件内的setter中发生了一些数据损坏。所以,现在代码实际上工作正常,现在我清理了它。感谢你们帮助我的所有努力! –

+0

@KrisH。我很高兴你已经找到真正的原因。 :) – shinkou

0

你能与此

int compare(const void *a, const void *b) // The comparison function for determining 
{           // alphabetic ordering. 
    const Person *p1 = *(const Person**)a; 
    const Person *p2 = *(const Person**)b; 
    return strcmp((p1)->name, (p2)->name); // Compare alphabetically, return result. 
} 
+0

我实际上得到了与原始代码相同的结果。从第一个元素到最后一个元素,我在排序后得到了Tanya,Alfred,Harry,Oakley ...... –

+0

除了'const'限定之外,没有什么值得惊讶的,这相当于。 –