Hi, I get a output result: Segmentation fault: 11 when I tried to do quick sort a time in ship structure
Here is my code
#include <stdio.h>
struct harbour
{
int harbourId;
int maxCap;
int timeUpload;
double amount;
};
struct ship
{
int number;
int shipId;
int sMaxCap;
int sActCap;
int aTime;
};
int sort(const void *x, const void *y)
{
const struct ship *const *ship1 = x;
const struct ship *const *ship2 = y;
if ( (*ship1)->aTime > (*ship2)->aTime ) return 1;
if ( (*ship1)->aTime < (*ship2)->aTime ) return -1;
return 0;
}
void readHarbour( struct harbour *dock)
{
int i=0, lineCount=0;
FILE *harbourf;
harbourf = fopen("data.txt","r");
do
{
fscanf(harbourf, "%d %d %d %lf", &dock[i].harbourId, &dock[i].maxCap, &dock[i].timeUpload, &dock[i].amount);
lineCount++;
i++;
} while (feof(harbourf) == 0);
//printf("%d\n", lineCount);
}
void readShip( struct ship *ship)
{
int i=0, lineCount=0;
FILE *shipf;
shipf = fopen("data2.txt","r");
do
{
fscanf(shipf, "%d %d %d %d", &ship[i].shipId, &ship[i].sMaxCap, &ship[i].sActCap, &ship[i].aTime);
lineCount++;
i++;
} while (feof(shipf) == 0);
// printf("%d\n", lineCount);
}
void processing(struct harbour h[100], struct ship s[100])
{
int i, lineCount =24;
qsort(s, lineCount, sizeof(*s), sort);
for(i=0;i<lineCount;i++)
{
printf("%d %d %d %d\n", s[i].shipId, s[i].sMaxCap, s[i].sActCap, s[i].aTime);
}
}
int main()
{
struct harbour h[100];
struct ship s[100];
readShip(s);
readHarbour(h);
processing(h, s);
return 0;
}