# qsort structs

Page 1 of 1

## 3 Replies - 854 Views - Last Post: 02 March 2013 - 07:47 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=313986&amp;s=6ab661acff18cecc11ea6da96b408425&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Gagzzy

• New D.I.C Head

Reputation: 0
• 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

• Duke of Err

Reputation: 1850
• Posts: 6,646
• Joined: 19-February 09

## Re: qsort structs

Posted 01 March 2013 - 04:56 PM

Does this work ok?

```28      struct dates *date[n];

```

### #3 baavgai

• Dreaming Coder

Reputation: 6966
• Posts: 14,572
• 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;
}

```

### #4 Salem_c

• void main'ers are DOOMED

Reputation: 2129
• Posts: 4,196
• 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.