不兼容的指针类型

不兼容的指针类型

我在编写我们应该编写的函数时遇到了一些问题。据说,这就是它应该如何工作,但它给了我不兼容的指针类型错误,我不知道如何解决它。

问题在于引用 compare_last 函数的 qsort。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_PERSONS 100

//person structure definition goes here
typedef struct{
    char last[32];
    char first[32];
    int year;
}Person;

//function declarations go here
int compare_last(Person * ptr1, Person * ptr2);

void main(void){//main function goes here
    char *infilename[20];
    char *outfilename[20];
    FILE * fptrin;
    FILE * fptrout;
    int i, j;
    Person musicians[MAX_PERSONS];
    printf("Enter input file name: ");
    scanf("%s", infilename);
    printf("Enter output file name: ");
    scanf("%s", outfilename);
    strcat(*outfilename, ".htm");
    fptrin = fopen(*infilename, "r");
    fptrout = fopen(*outfilename, "w");

    for(i = 0; i < MAX_PERSONS; i++)
    {
        fscanf(fptrin, "%s %s %i", musicians[i].last, musicians[i].first, &musicians[i].year);
    }

    qsort(musicians, i, sizeof(musicians[0]), compare_last);

    fprintf(fptrout, "<html>\n<body>\n<title>LAB14</title>\n");

    for(j = 0; j < i; j++)
    {
        fprintf(fptrout, "%s %s %i <br>", musicians[j].last, musicians[j].first, musicians[j].year);
    }

    fprintf(fptrout, "</body>\n</html>\n");
    fclose(fptrin);
    fclose(fptrout);

}//end main

//function definitions go here

int compare_last(Person * ptr1, Person * ptr2)
{
    int result = strcmp(ptr1 -> last, ptr2 -> last);
    if(result != 0)
        return result;
    else
        return strcmp(ptr1 -> first, ptr2 -> first);
}

最佳答案

int compare_last(Person * ptr1, Person * ptr2);

应该
int compare_last(void * ptr1, void * ptr2);

然后你需要在 compare_last 中进行转换

关于c - 从不兼容的指针类型传递 'qsort' 的参数 4,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5865570/

10-11 21:02