简体   繁体   English

C - “使用结构指针时出错”

[英]C - "error in working with structure pointers"

I have a function to which I send elements of an array structure, but, for some reason, an error constantly comes out我有一个 function,我向其发送数组结构的元素,但由于某种原因,错误不断出现

typedef struct student{
    char *fullName;
    char groupNumber[8];
    float GPA;
}student;
int cmpName(student* p1, student* p2){
    printf("%s\n",p1->fullName);
    return (strcmp(p1->fullName,p2->fullName));
}

void gnomeSort(student **mass,int size,int (*cmpFunc)(student *p1, student *p2)){
        int index = 0;
        while (index < size){
            if (index == 0)
                index++;
            if (cmpFunc(mass[index],mass[index-1]))
                index++;
            else{
                student *tmp;
                tmp = mass[index-1];
                mass[index-1] = mass[index];
                mass[index] = tmp;
                index--;
            }

        }
}

I understand that the problem is most likely in the pointers, but I do not know how to solve it我知道问题很可能在指针中,但我不知道如何解决

try not to make function in the arguments尽量不要在 arguments 中制作 function

void gnomeSort(student **mass,int size,int (*cmpFunc)(student *p1, student *p2))

replace int (*cmpFunc)(student *p1, student *p2) with int param and execute the function cmpName() independently, then call gnomeSort()int (*cmpFunc)(student *p1, student *p2)替换为int param并独立执行 function cmpName() ,然后调用gnomeSort()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM