首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(C)用bsearch在struct数组中按名称查找结构

(C)用bsearch在struct数组中按名称查找结构
EN

Stack Overflow用户
提问于 2021-03-27 19:49:16
回答 1查看 253关注 0票数 0

我正在尝试实现函数(1),以便在struct数组中按名称查找特定的产品。p_array是包含n个产品的结构数组,search_key是指向搜索结构名称的字符串的指针,cmp是指向我实现的另一个函数(2)的指针。

代码语言:javascript
复制
/* (1) */
const Product* findProduct(const Product* p_array, const char* search_key, int (*cmp)(const void*, const void*))
{
    return bsearch(&search_key, p_array, 100, sizeof(Product), cmp);
    
}

/* (2) */
int compareAlpha(const void* a, const void* b)
{
    const struct Product *shop_a = a;
    const struct Product *shop_b = b;

    int ret = strcmp(shop_a->name,shop_b->name);
    
    return ret;
}

我有以下问题:

我想不出一种方法来找出p_array

  • Launching的长度,程序导致了分段错误,我不知道为什么

我的主要职能是:

代码语言:javascript
复制
void printProducts(Product* array)
{
    int i = 0;
    while(array[i].name[0] != 0)
    {
        printf("product: %s\tprice: %f\t in stock: %d\n", array[i].name, array[i].price, array[i].in_stock);
        i++;
    }
}

int main()
{
    Product array[6] = {
    {"peanut butter", 1.2,  5},
    {"cookies",      12.3, 23},
    {"cereals",       3.2, 12},
    {"bread",     2.7, 12},
    {"butter",    4.2,  5},
    {"\0",      0.0, 0}
    };

    qsort(array, 5, sizeof(Product), compareAlpha);
    printf("sorted lexically:\n");
    printProducts(array);
    
    const Product* search = findProduct(array, "cookies", compareAlpha);
    if(search)
    {
        printf("Found product:\n");
        printf("%s\n", search->name);
    }

    qsort(array, 5, sizeof(Product), compareNum);
    printf("sorted by in stock:\n");
    printProducts(array);

    return 0;
}

想要的输出是

代码语言:javascript
复制
sorted lexically:
product: bread price: 2.700000 in stock: 12
product: butter price: 4.200000 in stock: 5
product: cereals price: 3.200000 in stock: 12
product: cookies price: 12.300000 in stock: 23
product: peanut butter price: 1.200000 in stock: 5
Found product:
cookies
Product not found!
sorted by in stock:
product: cookies price: 12.300000 in stock: 23
product: bread price: 2.700000 in stock: 12
product: cereals price: 3.200000 in stock: 12
product: butter price: 4.200000 in stock: 5
product: peanut butter price: 1.200000 in stock: 5
EN

回答 1

Stack Overflow用户

发布于 2021-03-28 00:32:29

您的代码中有几个错误:

  1. 转换在compareAlpha()函数中是不可能的。您应该编写const struct Product* shop_a = (const struct Product*)a;
  2. In您的printProducts()函数,您应该将array[i].name[0]与空字符'\0'进行比较,而不是0--即在printProducts()函数中的参数array,生成一个警告。您将array声明为静态数组,而不是作为指针(尽管它本身是实现的,并且可以作为指针访问),这将生成警告。相反,您应该像下面这样传递它-- void printProducts(Product array[]).
  3. In -- findProduct()函数,在调用bsearch()函数时,传递参数作为&search_key进行搜索。相反,您应该在没有&.
  4. The的情况下传递它,findProduct()的返回类型应该是void*,而不是const Product*,因为bsearch()函数的返回类型是void*。因此,在从const Product*.
  5. You函数获得返回值之后,需要将其键入到findProduct(),在某些地方使用Product类型时没有使用struct关键字。在c++中,它是可选的,但在C中它是强制性的。

  1. 我想不出一种方法来找出p_array

的长度

您可以找到类似于此int size = sizeof(array) / sizeof(array[0]);的数组的大小,尽管不建议这样做,因为它依赖于编译器。

  1. 启动该程序会导致段提错误,我不知道为什么

下面是完全工作的代码,上面提到的所有错误/警告都被纠正了.

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Product{
    char name[100];
    double price;
    int in_stock;
};

int compareAlpha(const void* a, const void* b)
{
    const struct Product* shop_a = (const struct Product*)a;
    const struct Product* shop_b = (const struct Product*)b;

    int ret = strcmp(shop_a->name, shop_b->name);

    return ret;
}

int compareNum(const void* a, const void* b)
{
    const struct Product* shop_a = (const struct Product*)a;
    const struct Product* shop_b = (const struct Product*)b;

    int ret = shop_a->in_stock <= shop_b->in_stock;

    return ret;
}

void* findProduct(struct Product p_array[], const char* search_key, int (*cmp)(const void*, const void*))
{
    return bsearch(search_key, (void*)p_array, 5, sizeof(struct Product), cmp);

}



void printProducts(struct Product array[])
{
    int i = 0;
    while(array[i].name[0] != '\0')
    {
        printf("product: %s\tprice: %f\t in stock: %d\n", array[i].name, array[i].price, array[i].in_stock);
        i++;
    }
}

int main()
{
    struct Product array[6] = {
        {"peanut butter", 1.2,  5},
        {"cookies",      12.3, 23},
        {"cereals",       3.2, 12},
        {"bread",     2.7, 12},
        {"butter",    4.2,  5},
        {"\0",      0.0, 0}
    };

    qsort(array, 5, sizeof(struct Product), compareAlpha);
    printf("sorted lexically:\n");
    printProducts(array);

    const struct Product* search = (const struct Product*)findProduct(array, "cookies", compareAlpha);
    if(search)
    {
        printf("Found product:\n");
        printf("%s\n", search->name);
    }else{
        printf("Product not found!\n");
    }

    qsort(array, 5, sizeof(struct Product), compareNum);
    printf("sorted by in stock:\n");
    printProducts(array);

    return 0;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66835479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档