I tried to sort an array of strings with qsort but got this warning:
warning: passing argument 4 of 'qsort' from incompatible pointer type
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_PRODUCTS 1000
int main (void) {
int i, index = 0, isUnique;
char products[MAX_PRODUCTS][100];
char product[100];
int count[MAX_PRODUCTS];
FILE * fp;
fp = fopen ("one.txt", "r");
// Read words from file and put in array if unique
while (fscanf(fp, "%s", product) != EOF){
isUnique = 1;
for (i=0; i<index && isUnique; i++){
if (strcmp(products[i], product) == 0){
isUnique = 0;
}
}
if (isUnique) {
strcpy(products[index], product);
index++;
}
else {
count[i - 1]++;
}
}
qsort(products, MAX_PRODUCTS, sizeof(char*), strcmp);
fclose(fp);
return 0;
}
I also tried a custom function to compare strings but this didn't worked either. What can I do to fix it?
qsort
is documented on the Microsoft website where it states:
compare Pointer to a user-supplied routine that compares two array elements and returns a value that specifies their relationship.
Use this:
int compare (const void * a, const void * b)
{
return strcmp((char*)a, (char*)b );
}
the following way:
qsort(products, MAX_PRODUCTS, 100, compare);