diff options
author | ꦌ ꦫꦶ ꦏ꧀ꦦꦿ ꦧ ꦮ ꦑ ꦩ ꦭ꧀ <erik@darapsa.co.id> | 2019-10-22 18:07:36 +0800 |
---|---|---|
committer | ꦌ ꦫꦶ ꦏ꧀ꦦꦿ ꦧ ꦮ ꦑ ꦩ ꦭ꧀ <erik@darapsa.co.id> | 2019-10-22 18:07:36 +0800 |
commit | e8ae8fbeab89beb43138e26465965d3510edd1c7 (patch) | |
tree | 887d77ce748eec7f6256947ee9d7cd506109b68d | |
parent | 6fa0df3c46986b15dec3be672f41eb7ea62c20ef (diff) |
Corrected qsort and bsearch usages
-rw-r--r-- | client.c | 25 |
1 files changed, 15 insertions, 10 deletions
@@ -43,32 +43,37 @@ void icclient_allproducts(size_t (*handler)(void *, size_t, size_t, void *) static int prodcmp(const void *product1, const void *product2) { - return strcmp(((icclient_product *)product1)->sku - , ((icclient_product *)product2)->sku); + return strcmp((*(icclient_product * const *)product1)->sku + , (*(icclient_product * const *)product2)->sku); } static int itemcmp(const void *item1, const void *item2) { - return strcmp(((icclient_ord_item *)item1)->product->sku - , ((icclient_ord_item *)item2)->product->sku); + return strcmp((*(icclient_ord_item * const *)item1)->product->sku + , (*(icclient_ord_item * const *)item2)->product->sku); } void icclient_order(icclient_ord_order **orderptr, const char *sku , icclient_catalog *catalog) { icclient_product **products = catalog->products; - qsort(products, catalog->length, sizeof(icclient_product), prodcmp); - icclient_product *product = bsearch(sku, products, catalog->length - , sizeof(icclient_product), prodcmp); + 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); + free(key->sku); + free(key); icclient_ord_order *order = *orderptr; icclient_ord_item *item = NULL; 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); + qsort(items, order->nitems, sizeof(icclient_ord_item *), itemcmp); + item = bsearch(sku, items, order->nitems + , sizeof(icclient_ord_item *), itemcmp); } else { *orderptr = malloc(sizeof(icclient_ord_order)); order = *orderptr; |