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;
}
Member Avatar for MonsieurPointer

Change

qsort(s, lineCount, sizeof(*s), sort);

to

qsort(s, lineCount, sizeof(struct ship), sort);

The third parameter requires the size of each element in the array, which is of type struct ship. Take a look at http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/

> Change sizeof(*s) to sizeof(struct ship) Care to elaborate what difference woud it make?

To OP:
The problem is that sort() parameters are struct ship * (disguised as void *), while you treat them as struct ship ** (notice the double indirection).

Member Avatar for MonsieurPointer

Care to elaborate what difference woud it make?

My bad, really overlooked that. :/

Seriously, you really lost me here:

const struct ship *const *ship1 = x;
const struct ship *const *ship2 = y;

Looks a little more complicated than it should.

int sort(const void *x, const void *y)
{
const struct ship
     *ship1 = (struct ship *)x,
     *ship2 = (struct ship *)y;
 
if ( ship1->aTime > ship2->aTime ) return 1;
if ( ship1->aTime < ship2->aTime ) return -1;
 
return 0;
}
commented: it useful +1

Than you both of you it really help ! thank

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.