Using malloc() & qsort with a union

This is a discussion on Using malloc() & qsort with a union within the C Programming forums, part of the General Programming Boards category; Help! I'm stuck & getting very frustrated, I have 2 questions. A file contains records of 3 varying lengths, hence ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    20

    Angry Using malloc() & qsort with a union

    Help! I'm stuck & getting very frustrated, I have 2 questions.

    A file contains records of 3 varying lengths, hence 3 structures and a union is used.

    union a_rec{
    struct a old;
    struct b delete;
    struct c new;
    };

    I have used malloc() to allocate some memory:

    pointer to start
    of memory = (union a_rec*)malloc(no_of_recs x Size);

    Q1. How do you now put the records from the file into this area of allocated memory?

    All the records (structures) have a code number which should be used to sort them - qsort is used to do this.

    qsort (ptr, no_of_recs, Size, compare);

    This is where I have a problem - the 'compare' function.

    int compare(const void *a, const void *b)
    {
    return(strcmp(((union a_rec*)a)->old.number, ((union a_rec*)
    b)->old.number))
    }

    Q2. The above asumes the record is (structure) 'old', but how do you compare the numbers no matter which type of record (structure) it is? There are 3 types in the file and this, to me, will only compare 'old' record numbers, what about 'delete' and 'new'?

  2. #2
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    What do the structure declarations look like?

    Also define an instance of the union. Haven't used unions before, but it would look like this for example:
    Code:
    union a_rec record; //a single union varable
    union a_rec *record; //pointer to type union
    union a_rec record[5]; //array of five union variables
    Also your malloc call looks a little off. Should be:
    Code:
    union a_rec *record = (union a_rec *) malloc (sizeof(union a_rec)*Size);
    If you give me the declarations, I'll enter some of this stuff in my compiler. I can probably figure out the qsort as well. Also is there a file? Just curious.
    I compile code with:
    Visual Studio.NET beta2

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    > Q2. The above asumes the record is (structure) 'old',
    qsort will only work (easily) if the key for the sort is common to all the variants.

    So your structure should look like this
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX_RECORDS 10
    
    struct a { int a; };
    struct b { char a; };
    struct c { float a; };
    
    typedef struct {
       int type;   // is it old,delete,new
       int code;   // unique code ID for the record
       union {
          struct a add;
          struct b delete;
          struct c new;
       }v;
    } rec_st;
    
    int compare ( const void *a, const void *b ) {
        const rec_st *pa = a;
        const rec_st *pb = b;
        if ( pa->code < pb->code ) return -1;
        if ( pa->code > pb->code ) return +1;
        return 0;
    }
    
    int main ( ) {
        rec_st  records[10];
        qsort( records, MAX_RECORDS, sizeof(rec_st), compare );
        return 0;
    }
    If the key is in the variant part of the record for some reason, then the compare function has to use the record 'type' in both the records in order to copy out the key to two local variables (or write out the 9 permutations of if/else logic), then compare those two keys.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. Still stuck using qsort + malloc!!
    By VegasSte in forum C Programming
    Replies: 16
    Last Post: 10-16-2002, 03:34 PM
  3. Need help with qsort() and union of structures
    By DanTheMan in forum C Programming
    Replies: 1
    Last Post: 04-02-2002, 07:30 AM
  4. Using malloc() & qsort with a union revisited
    By Cyber Kitten in forum C Programming
    Replies: 11
    Last Post: 11-24-2001, 05:07 PM
  5. Union In Malloc Sort
    By Delboy in forum C Programming
    Replies: 0
    Last Post: 10-16-2001, 06:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21