My struct is as such:

typedef struct pqueue {
	char str[20];
	int priority;
}pqueue;

pqueue q[MAXQ];

When I'm displaying the items in the priority queue, I am trying to sort it by priority, so I have the following comparison function:

int compareByPriority(const void *a, const void *b) {
	pqueue *pa = (pqueue *)a;
	pqueue *pb = (pqueue *)b;
	int p1, p2;
	p1 = pa->priority;
	p2 = pb->priority;
	return (p1 > p2) - (p1 < p2);
}

And this is my qsort() function:

qsort(q, MAXQ, sizeof(pqueue), compareByPriority);

However, the output, instead of displaying items in order of priority, I get a blank item output of priority 0. Where have I gone wrong? :X

Realized that the problem was due to the MAXQ. Have to set it to the number of elements present in the queue rather than the maximum number itself.

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.