summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorꦌ ꦫꦶ ꦏ꧀ꦦꦿ ꦧ ꦮ ꦑ ꦩ ꦭ꧀ <erik@darapsa.co.id>2019-10-22 22:31:31 +0800
committerꦌ ꦫꦶ ꦏ꧀ꦦꦿ ꦧ ꦮ ꦑ ꦩ ꦭ꧀ <erik@darapsa.co.id>2019-10-22 22:31:31 +0800
commit0bef9b7717ecff9544284f5ba4b7576dadad4892 (patch)
tree08cb77eac913235383e939a4b081ab0d937f7e9d
parente5b07c7117ddc511520c17a50aa762921c69db28 (diff)
Correct bsearch usage
-rw-r--r--client.c21
1 files 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;