How to qsort an array of pointers that use structs?

前端 未结 2 1571
醉梦人生
醉梦人生 2021-01-21 19:39

I want to sort an array of pointers by Id\'s. However qsort fails to work because of my lack of experience with pointers.

typedef struct block{
    int Id;
    c         


        
2条回答
  •  佛祖请我去吃肉
    2021-01-21 20:11

    You have an array of pointers to block, not an array of int. The sizeof argument is the size of the elements in the array, not the size of the data you want to compare.

    So the correct call would be e.g.

    qsort(database->data, database->size_array, sizeof *database->data, compare);
    

提交回复
热议问题