1

所以我试图在一个名为 conj_str 的字符串数组中进行二进制搜索,要做的是我必须对其进行排序,而我试图使用 qsort 的问题是比较函数不起作用并且它没有排序任何东西。

程序:

#include<stdlib.h> 
#include<stdio.h>
#include <string.h>
#define MAX_CHARS 1024
#define MAX_SIZE 10000

int compare (const void *a, const void *b)
{
  const char *key = a;
  const char * const *arg = b;
  return strcmp(key, *arg);
}

int main()
{
 int i;
 char conj_str[MAX_SIZE][MAX_CHARS];
 size_t len = sizeof(conj_str)/sizeof(const char *);
 strcpy(conj_str[0],"fcb");
 strcpy(conj_str[1],"bvb");
 strcpy(conj_str[2],"slb");
 strcpy(conj_str[3],"fcp");
 strcpy(conj_str[4],"rma");
 qsort (conj_str, len, sizeof (const char *), compare);
 for (i = 0; i < 5; i++) {
 printf ("%d: %s\n", i, conj_str[i]);
 }
}
4

2 回答 2

1

在这次通话中

qsort (conj_str, len, sizeof (const char *), compare);

错误地指定了数组元素的大小。必须有

qsort (conj_str, len, sizeof ( char[MAX_CHARS]), compare);

还有这个说法

int j = (int*) bsearch("fcb",conj_str,len,sizeof(const char *),compare);

没有意义。

在比较函数中这个声明

  const char * const *arg = b;

也没有意义。

该功能看起来像

int compare (const void *a, const void *b)
{
  const char *key = a;
  const char *arg = b;
  return strcmp(key, arg);
}

并替换此语句

size_t len = sizeof(conj_str)/sizeof(const char *);

为了

size_t len = 5;

这是您的程序,稍作改动。

#include<stdlib.h> 
#include<stdio.h>
#include <string.h>
#define MAX_CHARS 1024
#define MAX_SIZE 10000

int compare (const void *a, const void *b)
{
  const char *key = a;
  const char *arg = b;
  return strcmp(key, arg);
}

int main()
{
    int i;
    char conj_str[MAX_SIZE][MAX_CHARS];
    size_t len = 5;

 strcpy(conj_str[0],"fcb");
 strcpy(conj_str[1],"bvb");
 strcpy(conj_str[2],"slb");
 strcpy(conj_str[3],"fcp");
 strcpy(conj_str[4],"rma");
 qsort (conj_str, len, sizeof ( char[MAX_CHARS]), compare);
 for (i = 0; i < 5; i++) {
 printf ("%d: %s\n", i, conj_str[i]);
 }


 char ( *p )[MAX_CHARS] = bsearch("fcb",conj_str,len,sizeof(char[MAX_CHARS]),compare);

if ( p )
{
    printf( "Found at position %zu\n", ( size_t )(p - conj_str ) );
}
else
{
    puts( "Not found" );
}

}

它的输出是

0: bvb
1: fcb
2: fcp
3: rma
4: slb
Found at position 1
于 2020-05-19T17:16:00.367 回答
1

这里是你的代码的工作重写:

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

#define MAX_CHARS       1024
#define MAX_SIZE        5

int compare(const void *a, const void *b)
{
        return strcmp(a, b);
}

int main(void)
{
        int i;

        char conj_str[MAX_SIZE][MAX_CHARS];
        size_t len = sizeof(conj_str) / sizeof(conj_str[0]);
        strcpy(conj_str[0], "fcb");
        strcpy(conj_str[1], "bvb");
        strcpy(conj_str[2], "slb");
        strcpy(conj_str[3], "fcp");
        strcpy(conj_str[4], "rma");

        qsort(conj_str, len, sizeof(conj_str[0]), compare);

        for (i = 0; i < len; i++) {
                printf ("%d: %s\n", i, conj_str[i]);
        }

        char *s = bsearch("fcb", conj_str, len, sizeof(conj_str[0]), compare);
        puts(s ? "found" : "not found");
}

第一件事:数组中的每个字符串都应该初始化(至少为空字符串)以便工作qsort()bsearch()这就是我输入的原因:

#define MAX_SIZE        5

(and also because 10000 * 1024 are too much for an array on the stack)

then the size argument of both qsort() and bsearch() was wrong, in this case should be:

sizeof(conj_str[0])

since it's the size of the elements contained in the array.

bsearch() returns a pointer to the element if founded, so char *. The last line should be:

puts(s ? "found" : "not found");

where s is the return pointer of bsearch().

于 2020-05-19T17:30:54.443 回答