简体   繁体   中英

qsort() from stdlib.h is not sorting a spezified structure containing zip-codes and corrresponding city

I'm using this gloabal crisis to study C beside my studies and i am following a german book (Rheinwerk Computing: C von A bis Z, ANSI-C99) and finally reached searching functions. As a practice I wanted to implement a sorting function to provide the functionallity of a binaray search.

My problem is, that whenever i add a new zip-structure they won't get sorteted. I was looking through different qestions here already and found out how to build a comparsion function for structures and how to handle structs with qsort. But it seems like that the answers didn't help or i am probably to dump to see it.

Technical enviorment: I use vim and gcc (FLAGS: -Wpedantic -Wstrict-aliasing -Wshadow -Wcast-align -Wextra -Wfloat-equal) on a 32-Bit debian subsystem for windows. Since gcc is not complaining about anything while compiling the code, i wonder what i made wrong.

My code:

struct zipc
{
   char place[255];
   unsigned int zip;
};

struct zipc zipcodes[100];

static int N; //Counting how many adresses are already typed in

// create new zip with city, init function creates one with N=0 and dummy values

void insertbs(unsigned int p, char *o) 
{
    zipcodes[++N].zip = p;
    strcpy(zipcodes[N].place, o);
}

int cmp_uinteger(const void *value1, const void *value2)
{
   const struct zipc *p1 = value1;
   const struct zipc *p2 = value2;

   if(p1->zip > p2->zip) 
        return 1;
   if(p2->zip > p1->zip)
        return (-1);
   else 
        return 0;
}

//usage of qsort later in main

qsort(zipcodes, N-1, sizeof(unsigned int), cmp_uinteger);

The cmp_uinteger function is inspired by annother question here and seems to be pretty nice but the sort always fails, but not in a way that the program fails. The program is running but if one inserts zip codes in abitrary order, binary search fails, that's why i thinks it's about qsort() but i although can be somewhere else. I think i don't really get the point for the comparsion function maybe somebody can explain that too.

It feels strange to post my first question here because stack overflow become my second home during the C tutorial:D

Best greets, Maleware

qsort(zipcodes, N-1, sizeof(unsigned int), cmp_uinteger);

Third argument of qsort() "Size in bytes of each element in the array" .

In your case it should be sizeof(struct zipc) instead of sizeof(unsigned int) .

With a dummy value at zipcodes[0] , sort from zipcodes + 1 and use the size of an array element - easier to code right, review and maintain than attempting to code the size of a type (which was the wrong type in OP's code)

// qsort(zipcodes, N-1, sizeof(unsigned int), cmp_uinteger);
qsort(zipcodes + 1, N-1, sizeof zipcodes[0], cmp_uinteger);

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