C:获取数组类型结构成员的数组

问题描述

考虑以下结构

typedef struct{
    int day;
    int month;
    int year;
    char location[strlength];
    float timeInHours;
}appointment;

我想使用 qsort 按字母顺序对多个约会进行排序。因此,我尝试获取一个包含我所有约会的所有位置的数组,然后将其提供给 qsort。但是,我的代码不起作用,控制台只是冻结了几秒钟,然后我得到了一个随机返回码。

void sortLoc(appointment* x,int length,int size){

//length = number of appointments,size = carries sizeof(appointment)
    
    char* locs[strlength];
    *locs = malloc(length*sizeof(locs[strlength]));
    int i = 0;
    for(i = 0; i < length; i++){
        strcpy((char*)(locs+i),x[i].location);  
    }

    for(i = 0; i < length; i++){
        printf("%s\n",&(locs[i])); //this acutally outputs the right locations,so the extraction kinda worked
    }

    qsort((void*) (&locs),length,sizeof(locs),(void*) strcmp);
    return;
}

问题在于我调用 qsort 的方式,因为如果不调用它,该方法就可以正常工作。但我不知道如何以一种有效的方式将我的数组提供给 qsort。

感谢您的帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)