From 0bef9b7717ecff9544284f5ba4b7576dadad4892 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=20=EA=A6=AB=EA=A6=B6=20=EA=A6=8F=EA=A7=80?= =?UTF-8?q?=EA=A6=A6=EA=A6=BF=20=EA=A6=A7=20=EA=A6=AE=20=EA=A6=91=20?= =?UTF-8?q?=EA=A6=A9=20=EA=A6=AD=EA=A7=80?= Date: Tue, 22 Oct 2019 22:31:31 +0800 Subject: Correct bsearch usage --- client.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/client.c b/client.c index 87cb90a..aa14da0 100644 --- a/client.c +++ b/client.c @@ -61,12 +61,13 @@ void icclient_order(icclient_ord_order **orderptr, const char *sku { icclient_product **products = catalog->products; qsort(products, catalog->length, sizeof(icclient_product *), prodcmp); - icclient_product *key = malloc(sizeof(icclient_product)); - key->sku = malloc(strlen(sku) + 1); - strcpy(key->sku, sku); - icclient_product *product = bsearch(&key, products, catalog->length - , sizeof(icclient_product *), prodcmp); - icclient_freeproduct(key); + icclient_product *key_product = malloc(sizeof(icclient_product)); + key_product->sku = malloc(strlen(sku) + 1); + strcpy(key_product->sku, sku); + icclient_product *product = *(icclient_product **)bsearch(&key_product + , products, catalog->length, sizeof(icclient_product *) + , prodcmp); + icclient_freeproduct(key_product); icclient_ord_order *order = *orderptr; icclient_ord_item *item = NULL; @@ -74,8 +75,12 @@ void icclient_order(icclient_ord_order **orderptr, const char *sku if (order && order->nitems) { icclient_ord_item **items = order->items; qsort(items, order->nitems, sizeof(icclient_ord_item *), itemcmp); - item = bsearch(sku, items, order->nitems - , sizeof(icclient_ord_item *), itemcmp); + icclient_ord_item *key_item = malloc(sizeof(icclient_ord_item)); + key_item->product = product; + item = *(icclient_ord_item **)bsearch(&key_item, items + , order->nitems, sizeof(icclient_ord_item *) + , itemcmp); + free(key_item); } else { *orderptr = malloc(sizeof(icclient_ord_order)); order = *orderptr; -- cgit v1.2.3