3 Replies - 321 Views - Last Post: 02 March 2013 - 07:47 AM Rate Topic: -----

#1 Gagzzy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 22-April 12

qsort structs

Posted 01 March 2013 - 03:32 PM

hi i am trying to use qsort on a structure holding dates, i am trying to sort years out first but not having much luck i have in my code incompatible pointer... it is to do with my qsort in main can anyone tell me what am doing wrong please

struct dates
{
    int index;
    int day;
    int year;
    char month[15];
};

int cmp(const struct dates* p1, const struct dates* p2)
{
    if(p1 -> year < p2 -> year)
    {
        return -1;
    }
    else if(p1 -> year > p2 -> year)
    {
        return 1;
    }
    else return 0;
}

int main() {
    
    int n;

    scanf("%d", & n);

    struct dates *date[n];

    for(int i = 0; i < n; i++)
    {
        date[i] = (struct dates*)malloc(sizeof(struct dates));
    
        scanf("%s %d2 %d2", date[i]->month, & date[i]->day, & date[i]->year);

    }

    qsort(date, n, sizeof(*date), cmp);
    
    for(int i=0; i<n; i++)
    {
        printf("%s %d2 %d2\n\n",date[i]->month, date[i]->day, date[i]->year);
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: qsort structs

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1348
  • View blog
  • Posts: 4,645
  • Joined: 19-February 09

Re: qsort structs

Posted 01 March 2013 - 04:56 PM

Does this work ok?

28      struct dates *date[n];


Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5840
  • View blog
  • Posts: 12,694
  • Joined: 16-October 07

Re: qsort structs

Posted 01 March 2013 - 05:51 PM

But you don't have an array of structs, you have an array of pointers. Why the hell would you do that?

Here's a simple example of sorting structs.
#include <stdlib.h>
#include <stdio.h>

typedef struct {
	int x, y;
} Point;

int cmpPoint(const void *pt1, const void *pt2) {
	int cmp = ((Point *) pt1)->x - ((Point *) pt2)->x;
	return (cmp == 0) ? ((Point *) pt1)->y - ((Point *) pt2)->y : cmp;
}

int main() {
	Point points[] = { {5, 6}, {3, 4}, {2, 1}, {1, 1}, {3, 2} 	};
	int i, size = sizeof (points) / sizeof (points[0]);

	qsort(points, size, sizeof (Point), cmpPoint);
	for (i = 0; i < size; i++) { printf("(%d,%d)", points[i].x, points[i].y); }
	printf("\n");
	
	return 0;
}



Since you're making it harder on yourself...
int cmpPointPtr(const void *ppt1, const void *ppt2) {
	Point *pt1 = *((Point **)ppt1);
	Point *pt2 = *((Point **)ppt2);
	int cmp = pt1->x - pt2->x;
	return (cmp == 0) ? pt1->y - pt2->y : cmp;
}

int main() {
	Point points[] = { {5, 6}, {3, 4}, {2, 1}, {1, 1}, {3, 2} 	};
	Point *pp[10];
	int i, size = sizeof (points) / sizeof (points[0]);

	for (i = 0; i < size; i++) { pp[i] = points + i; }
	qsort(pp, size, sizeof (Point *), cmpPointPtr);
	for (i = 0; i < size; i++) { printf("(%d,%d)", pp[i]->x, pp[i]->y); }
	printf("\n");

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: qsort structs

Posted 02 March 2013 - 07:47 AM

> int cmp = ((Point *) pt1)->x - ((Point *) pt2)->x;
Beware of arithmetic underflow giving you an unexpected answer.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1