简体   繁体   中英

Sorting an array of structs using qsort

I am new to C, so I apologize if there are any obvious errors. I want to sort this list in alphabetical order using qsort . I came up with this, but my sort function doesn't return anything for some reason. No error, it's just blank.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// struct person with 3 fields
struct People {
    char* name;
    char age;
};
static int myCompare(const void* a, const void* b)
{
 
    // setting up rules for comparison
    return strcmp(*(const char**)a, *(const char**)b);
}
// Function to sort the array
void sort(const char* arr[], int n)
{
    // calling qsort function to sort the array
    qsort(arr, n, sizeof(const char*), myCompare);
}

int main()
{
    int i = 0, n = 17;

    struct People arr[n];

    // Make people list
    arr[0].name = "Bell";
    arr[0].age = 20;

    arr[1].name = "Tan";
    arr[1].age = 31;

    arr[2].name = "Jones";
    arr[2].age = 19;

    // Sort the given names
    sort(arr, n);

    // Print the sorted names
    printf("\nSorted array is\n");
    for (i = 0; i < n; i++)
        printf("%d: %s \n", i, arr[i]);


    return 0;
}

There are many issues. I have the strong impression you have tried to blindly adapt some code that was supposed to sort pointers to char* without understanding the basics.

You want this, explanations in the comments:

static int myCompare(const void* a, const void* b)
{
  return strcmp(((struct People *)a)->name, ((struct People*)b)->name);
}

void sort(struct People arr[], int n)              // you want to sort an array
                                                   // of struct People
{
  // calling qsort function to sort the array
  qsort(arr, n, sizeof(struct People), myCompare); // the size of your elements is
                                                   // sizeof(struct People), 
                                                   // not size of (char*)
}

int main()
{
  int i = 0, n = 3;          // you don't have 17 elements, you only have 3
                             // With the original value 17 you would sort
                             // 17 elements, but your array contains
                             // only 3 elements, the other 14 ones
                             // are uninitialized

  struct People arr[n];

  // Make people list
  arr[0].name = "Bell";
  arr[0].age = 20;

  arr[1].name = "Tan";
  arr[1].age = 31;

  arr[2].name = "Jones";
  arr[2].age = 19;

  // Sort the given names
  sort(arr, n);

  // Print the sorted names
  printf("\nSorted array is\n");
  for (i = 0; i < n; i++)
    printf("%d: %s \n", i, arr[i].name);   // you want to print the name,
                                           // not a struct People

  return 0;
}

Explanation for ((struct People *)a)->name in myCompare .

The comparision function has 3 parametes,the first and the second ones are pointers to the elements to be compared, the third one is the size of an element.

(struct People *)a) casts the void pointer a to a pointer to struct People . The -> operator accesses the name field.

If you want to sort by age all you need to to is replace

strcmp(((struct People *)a)->name, ((struct People*)b)->name)

with

((struct People *)a)->age < ((struct People*)b)->age

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM