gpt4 book ai didi

c - 比较功能中的 Bsearch 和垃圾值

转载 作者:行者123 更新时间:2023-11-30 19:09:58 25 4
gpt4 key购买 nike

我有以下程序:

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

#define DICT_BUFSIZE 64

int compar(const void * a, const void * b)
{
const char* c1 = (const char*)a;
const char* c2 = (const char*)b;

printf("c1: %s | c2: %s\n", c1, c2);

return strcmp(c1, c2);
}

int main (void)
{
FILE* fdict;
uint32_t i;
char** dict = NULL;
size_t size = 0;
size_t size_alloced = 0;
char buf[DICT_BUFSIZE];

fdict = fopen("/usr/share/dict/words", "r");
if (!fdict) {
printf("Could not open \"%s\": %s\n", "usr/share/dict/words", strerror(errno));
exit(1);
}

for (i = 0; fgets(buf, DICT_BUFSIZE, fdict); ++i) {
size_t len;

if (i == size_alloced) {
dict = realloc(dict, (i +50000) * sizeof(*dict));
size_alloced += 50000;
}
len = strlen(buf);
dict[i] = malloc(len);

memcpy(dict[i], buf, len -1);
dict[i][len -1] = '\0';
}
size = i;

//for (i = 0; i < size; i++)
//printf("%s\n", dict[i]);

if(bsearch("company", dict, size, sizeof(*dict), compar))
printf("Found!\n");

for (i = 0; i < size; ++i)
free(dict[i]);
free(dict);

fclose(fdict);

return 0;
}

在“compar”函数中,“c1”变量(要搜索的键)正确显示,但 v2 变量中有垃圾输出。

这是一个示例输出:

c1: company | c2: ���
c1: company | c2: �$z
c1: company | c2: ��I
c1: company | c2: ��7
c1: company | c2: P�.
c1: company | c2: �b3
c1: company | c2: �1
c1: company | c2: P�/
c1: company | c2: ��0
c1: company | c2: PC0
c1: company | c2: @g0
c1: company | c2: y0
c1: company | c2: 0�0
c1: company | c2: ��0
c1: company | c2: `�0
c1: company | c2: ��0
c1: company | c2:
c1: company | c2: P�0

我无法理解这种行为。

最佳答案

如果您正在搜索 int 的数组,您将转换 const void *int * 进行比较函数的参数,你不愿意吗?

您正在搜索 char * 的数组,所以您需要转换 const void * char ** 的参数— 并且您需要传递 char **要查找的值的参数。

您的代码所需的更改很少但至关重要:

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

#define DICT_BUFSIZE 64

static int compar(const void *a, const void *b)
{
const char *c1 = *(const char **)a;
const char *c2 = *(const char **)b;

printf("c1: %s | c2: %s\n", c1, c2);

return strcmp(c1, c2);
}

int main (void)
{
FILE* fdict;
uint32_t i;
char** dict = NULL;
size_t size = 0;
size_t size_alloced = 0;
char buf[DICT_BUFSIZE];
const char *file = "/usr/share/dict/words";

fdict = fopen(file, "r");
if (!fdict) {
printf("Could not open \"%s\": %s\n", file, strerror(errno));
exit(1);
}

for (i = 0; fgets(buf, DICT_BUFSIZE, fdict); ++i) {
size_t len;

if (i == size_alloced) {
dict = realloc(dict, (i +50000) * sizeof(*dict));
size_alloced += 50000;
}
len = strlen(buf);
dict[i] = malloc(len);

memcpy(dict[i], buf, len -1);
dict[i][len -1] = '\0';
}
size = i;

//for (i = 0; i < size; i++)
//printf("%s\n", dict[i]);

const char *search = "company";
if(bsearch(&search, dict, size, sizeof(*dict), compar))
printf("Found!\n");

for (i = 0; i < size; ++i)
free(dict[i]);
free(dict);

fclose(fdict);

return 0;
}

比较器函数现在需要两个 char **值,并捕获每个值指向的字符串。

调用的第一个参数必须是 char * 的地址多变的;因此添加变量 const char *search = "company"; .

较小的清理包括使比较器函数静态(主要是为了满足我迂腐的默认编译选项 - 尽管最好在定义函数之前声明函数),以及使用变量 const char *file = "/usr/share/dict/words";避免对 fopen() 的调用之间(接近)重复以及错误消息。

示例输出(在装有 macOS Sierra 10.12.3 的 Mac 上运行):

c1: company | c2: modifier
c1: company | c2: eagle
c1: company | c2: Canarian
c1: company | c2: counteridea
c1: company | c2: citropten
c1: company | c2: compulsoriness
c1: company | c2: coelenteric
c1: company | c2: Colossian
c1: company | c2: commonable
c1: company | c2: compilation
c1: company | c2: compagination
c1: company | c2: compatriot
c1: company | c2: comparition
c1: company | c2: comparable
c1: company | c2: companionate
c1: company | c2: companionway
c1: company | c2: comparability
c1: company | c2: company
Found!

关于c - 比较功能中的 Bsearch 和垃圾值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42171645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com