c - qsort a dynamic array -


i using dynamic array found online header file looks this:

    typedef struct {     int size;      // slots used far     int capacity;  // total available slots     void **data;     // array of data we're storing     } vector;      void vector_init(vector *vector);      void vector_append(vector *vector, void* value);      void* vector_get(vector *vector, int index);      void vector_set(vector *vector, int index, void* value);      void vector_double_capacity_if_full(vector *vector);      void vector_free(vector *vector); 

i have struct looks this:

typedef struct _item{     char name[maxnamesize];     float price; } item; 

i have created vector using

vector vector; vector_init(&vector); 

and added number of items using

vector_append(&vector, item); 

multiple times item of type item. want sort dynamic array item price. have tried doesn't work

qsort(vector.data, vector.size, sizeof(item*), compare); 

my compare function is

int compare(const void* a, const void* b){ item* first = (item*)a; item* second = (item*)b;     return first.price - second.price; } 

any ideas of going wrong? think may arguments putting qsort. right seems skip on qsort.

your declaration of vector includes pointer-to-pointer-to-void data bed (void**). without seeing rest of code, must assume means you're managing vector of pointers, each pointer refers distinct object by-address. addresses you're receiving in compare offset addresses data pointer array. i.e. actual type void** passed void*.

therefore, you're missing level of indirection. syntax wrong regardless (ex: first->price, not first.price) doesn't help, if right, premise have, item* passed void* wrong.

your comparator should this, assuming rest of code correct:

int compare(const void* a, const void *b) {     const item * first = *(const void * const *)a;     const item * second = *(const void * const *)b;     return first->price < second->price ? -1 : second->price < first->price; } 

again, making pretty big assumptions code you're not providing, , there no guarantees work unless code solid , fulfills assumptions made above. if you're managing vector described above, should work.

i've taken liberty fix comparison itself. subtracting values may appear work integral types, wheels fall off floating point when converted final int result, or integral types if introduce underflow potential. trivial example:

#include <stdio.h>  int main() {     printf("%d\n", (int)(1.1f - 1.0f)); // wrong     printf("%d\n", (1.1f < 1.0f ? -1 : 1.0f < 1.1f)); // better     return 0; } 

output

0 1 

it can end in implementation-defined land if converting larger-integral-type value (suppose subtracting long long , result less int_min). sticking form showed avoids pitfall

if (a < b) return -1; return (b < a); // 1 or 0, depending on whether b < or not. 

thus comparator returns -1, 0, or 1 , conforms requirements of qsort whilst doing , avoiding inaccuracy of floating point int conversion or potential underflow/overflow.

best of luck.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -