1

I have been stuck on this for a while and nothing seems to work.

I have a data structure:

DATA
{
  int size;
  int id;
}

And I have an array of DATA structures:

myArray = (DATA *) malloc(10 * sizeof(DATA));

Then I assign some test values:

myArray[0].size = 5;
myArray[1].size = 9;
myArray[2].size = 1;
myArray[3].size = 3;

So my starting array should look like:

5,9,1,3,0,0,0,0,0,0

Then, I call qsort(myArray,10,sizeof(DATA),comp)

Where comp is:

  int comp(const DATA * a, const DATA * b)
{
    return a.size - b.size;
}

And trust me, I tried many things with the compare function, NOTHING seems to work. I just never get any sorting that makes any sense.

5
  • 1
    Can you post a full runnable sample? Devil is likely in the details you omit.
    – themel
    Commented Nov 15, 2011 at 8:35
  • 2
    did you initialize other elements of myArray? or are you just assuming that the uninitialized values will be zero?
    – A. K.
    Commented Nov 15, 2011 at 8:37
  • 2
    When allocating multiple elements, if you want to be sure that the memory is "zeroed" you'd better use calloc: calloc(10, sizeof(DATA)); malloc does not assure you to have them set to zero.
    – Teudimundo
    Commented Nov 15, 2011 at 8:39
  • 1
    How do you call qsort? Commented Nov 15, 2011 at 8:41
  • @Teudimundo Why don't you make that into an answer instead? Commented Nov 15, 2011 at 8:42

1 Answer 1

6

So my starting array should look like 5, 9, 1, 3, 0, 0, 0, 0, 0, 0.

No, it really won't, at least it's not guaranteed to.

If you want zeros in there, either use calloc() to zero everything out, or put them in yourself. What malloc() will give you is a block of the size required that has indeterminant content. In other words, it may well have whatever rubbish was in memory beforehand.

And, on top of that, a and b are pointers in your comp function, you should be using -> rather than . and it's good form to use the correct prototype with casting.

And a final note: please don't cast the return from malloc in C - you can get into problems if you accidentally forget to include the relevant header file and your integers aren't compatible with your pointers.

The malloc function returns a void * which will quite happily convert implicitly into any other pointer.


Here's a complete program with those fixes:

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

typedef struct {int size; int id;} DATA;

int comp (const void *a, const void *b) {
    return ((DATA *)a)->size - ((DATA *)b)->size;
}

int main (void) {
    int i;
    DATA *myArray = malloc(10 * sizeof(DATA));
    myArray[0].size = 5;
    myArray[1].size = 9;
    myArray[2].size = 1;
    myArray[3].size = 3;
    for (i = 4; i < 10; i++)
        myArray[i].size = 0;

    qsort (myArray, 10, sizeof(DATA), comp);
    for (i = 0; i < 10; i++)
        printf ("%d ", myArray[i].size);
    putchar ('\n');

    return 0;
}

The output:

0 0 0 0 0 0 1 3 5 9
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.