2015-07-22 44 views
1

在C中使用bsearch未能在結構數組中找到字符串'Eva Lam'。該數組按字符串成員的降序排序。我檢查了很多次,仍然不知道錯誤在哪裏?順便說一句,我正在使用DEV C++ 5.9.4。請幫助,非常感謝。bsearch未能在結構數組中找到字符串成員

#include <stdio.h> 
#include <stdlib.h>  // for bsearch 
#include <string.h> 
#define SIZE 4 
#define NAME_SIZE 20 

struct student { 
    int id; 
    char name[NAME_SIZE]; 
}; 

// Function prototypes 
int comp_name(const void* a, const void* b); 
void print_struct_array(struct student studs[], int size, int serial); 

int main(){ 
    int i, option=0; 
    struct student *stud, *target; 

    // studs array already sort in descending order of name 
    struct student studs[SIZE] = {{14123456, "Mary Chan"} 
    , {110, "Eva Lam"} 
    , {1, "David Wong"} 
    , {12345678, "Chris So"} 
    }; 

    printf("*** Before Searching ***\n"); 
    print_struct_array(studs, SIZE, 1); 

    target = (struct student*) malloc(sizeof(struct student)); 
    if (target == NULL) { 
    fprintf(stderr, "Out of memory!\n"); 
    return -1; 
    } 

    printf("Input student name to search: "); 
    scanf("%[^\n]", target->name); 
    fflush(stdin); 
    printf("name=%s\n", target->name); 


    stud = (struct student *)bsearch(target->name, studs, SIZE, 
    sizeof(struct student), comp_name); 
    if (!stud) 
    printf("name %s not found!\n", target->name); 
    else 
    printf("[id, name] found is [%d, %s]\n", stud->id, stud->name); 


    return 0; 
} 

int comp_name(const void* a, const void* b) { 
    printf("comp_name: a->name=%s, b->name=%s\n", 
    (*(struct student *)a).name, (*(struct student *)b).name); 
    return strcmp((*(struct student *)b).name, 
    (*(struct student *)a).name); 
} 


void print_struct_array(struct student studs[], int size, int serial) { 
    int i; 

    printf("Student array #%d is {\n", serial); 
    for (i=0; i<SIZE; i++) { 
    if (i==0) 
     printf(" "); 
    else if (i<=SIZE-1) 
     printf(", "); 
    printf("[%d, %s]\n", studs[i].id, studs[i].name); 
    } 
    printf("}\n"); 
} 

但搜索「伊娃林」時,該程序的輸出是:

*** Before Searching *** 
Student array #1 is { 
    [14123456, Mary Chan] 
, [110, Eva Lam] 
, [1, David Wong] 
, [12345678, Chris So] 
} 
Input student name to search: Eva Lam 
name=Eva Lam 
comp_name: a->name=Lam, b->name=Eva Lam 
comp_name: a->name=Lam, b->name=Mary Chan 
name Eva Lam not found! 

-------------------------------- 
Process exited after 8.216 seconds with return value 0 
+0

刪除'fflush(stdin);',這是未定義的行爲。 –

回答

1

更仔細地閱讀文檔bsearch

該compar例程預計有兩個參數指向按鍵對象和數組成員,按順序。

這意味着您的比較函數的第一個參數將始終與您作爲bsearch的第一個參數給出的值相同。但bsearch(target, studs, ...)或更好的,重寫你的比較函數:所以或者把它作爲

int 
comp_name(const void *av, const void *bv) { 
    const char *a = av; 
    const struct student *b = bv; 
    printf("comp_name: a->name=%s, b->name=%s\n", a, b->name); 
    return strcmp(b->name, a); 
} 

而且,請不要投從bsearch用C void *指針,特別是從malloc,而且還返回值在你的代碼。

+0

您對bsearch compar參數的解釋非常明確且有用。現在,我完全明白它的用法。你的建議奏效,非常感謝藝術。但是爲什麼不在C中使用void *指針,特別是從malloc中,還要從bsearch返回值? – SJ0407

+0

因爲沒有理由將void指針指向其他指針。如果您忘記包含正確的函數聲明,它可以隱藏代碼中的錯誤。 – Art