0

I have to solve several C problems and most of them involve having to use qsort() somewhere but no matter how much I take help from the net I can't get it to work. Take this code for example:

#include <stdio.h>
#include <string.h>
struct date
{
    int day;
    int month;
    int year;
};struct date d[5]={
    {12,12,2012},
    {23,02,2014},
    {31,01,2222},
    {32,21,2011},
    {12,01,1990}
    };

int compare(const void * a, const void * b)
{
    struct date *orderA = (date *)a;
  struct date *orderB = (date *)b;

  return (  orderA->year -orderB->year  );
}
int main()
{
    int i;
    qsort(d,5,sizeof(date),compare);

    for(i=0;i<5;i++)
    printf("%d %d %d\n",d[i].day,d[i].month,d[i].year);
    return 0;

}

I get errors that date is undeclared even though it is already. And I can't understand compare functions at all and have to copy them from the net. Help me please. My teacher at college is a total imbecile.

5
  • 2
    sizeof(struct date), also #include <stdlib.h> Commented Sep 16, 2016 at 17:01
  • 1
    And use struct date* orderA = (struct date*)a; in the compare function. Commented Sep 16, 2016 at 17:04
  • 2
    or use typedef struct date date; Commented Sep 16, 2016 at 17:05
  • The compare function is pretty simple. Return something less than 0 if the first argument comes before the second in the final array. Return 0 if both arguments are "equal". Return something greater than 0 if the first argument comes after the second in the final array. Let me know if I can help clear that up any more. Commented Sep 16, 2016 at 17:24
  • In C++, you can use the simple name date after defining struct date, but in C you must add typedef struct date date; to be able to use the unqualified type name date. Commented Sep 17, 2016 at 5:33

2 Answers 2

1

date is not a type. struct date is. You need to use the struct keyword when referencing a structure type.

Also, if you define the pointers in your comparison function as const the cast isn't needed.

const struct date *orderA = a;
const struct date *orderB = b;
Sign up to request clarification or add additional context in comments.

1 Comment

@overloood glad I could help. Feel free to accept this answer if you found it usefull.
0

Minor issues with your code: you need to include stdlib.h (for qsort()) but you're not using string.h in your example; you use date as a struct and a type but don't define it that -- you can define it as both at the same time via typedef; you can expand your comparision beyond years if you want.

A rework of your code addressing the above issues:

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

typedef struct date {
    int day;
    int month;
    int year;
} Date;

int compare(const void *a, const void *b) {
    const Date *date_A = a;
    const Date *date_B = b;

    return date_A->year == date_B->year ? 
        date_A->month == date_B->month ?
            date_A->day - date_B->day
                : date_A->month - date_B->month
                    : date_A->year - date_B->year;
}

int main() {

    Date dates[] = {
        {12, 12, 2012},
        {23, 02, 2014},
        {31, 01, 2222},
        {32, 21, 2011},
        {12, 01, 1990}
    };

    size_t count = sizeof(dates) / sizeof(Date);

    qsort(dates, count, sizeof(Date), compare);

    for (int i = 0; i < count; i++) {
        Date *date = &dates[i];
        printf("%d %d %d\n", date->day, date->month, date->year);
    }

    return 0;
}

Comments

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.