bsearch未能在结构数组中找到字符串成员

时间:2015-07-22 07:02:41

标签: c arrays bsearch

在C中使用bsearch未能找到字符串' Eva Lam'在一个结构数组中。此数组按字符串成员的降序排序。我多次检查过,仍然不知道这个bug在哪里?顺便说一下,我正在使用DEV C ++ 5.9.4。请多多帮助,谢谢。

#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"}
    , {11001234, "Eva Lam"}
    , {10123456, "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");
}

但是在搜索“Eva Lam”时,该程序的输出结果为&#39;是:

*** Before Searching ***
Student array #1 is {
  [14123456, Mary Chan]
, [11001234, Eva Lam]
, [10123456, 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

1 个答案:

答案 0 :(得分:1)

仔细阅读public class logout extends HttpServlet { public void service(HttpServletRequest rq, HttpServletResponse rs) throws IOException, ServletException { try { rs.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); rs.addHeader("Cache-Control", "post-check=0, pre-check=0"); rs.setHeader("Pragma", "no-cache"); rs.setDateHeader("Expires", 0); HttpSession ss = rq.getSession(false); if (ss.getAttribute("uid") == null) { rs.sendRedirect("/"); } ss.removeAttribute("uid"); if (ss != null) { ss.invalidate(); } rs.sendRedirect("/"); } catch (Exception exp) { // rs.sendRedirect("/"); RequestDispatcher dd = rq.getRequestDispatcher("/"); dd.forward(rq, rs); } } } 的文档。

  

compar例程应该有两个参数,它们按顺序指向键对象和数组成员。

这意味着比较函数的第一个参数将始终与bsearch的第一个参数完全相同。因此,要么将其称为:bsearch,要么更好,将比较函数重写为:

bsearch(target, studs, ...)

此外,请不要在C中投射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); } 指针,尤其是来自void *,还要在代码中投放malloc的返回值。