Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 根据结构内部的元素对指向结构的指针数组进行排序_C_Arrays_Sorting_Pointers_Struct - Fatal编程技术网

C 根据结构内部的元素对指向结构的指针数组进行排序

C 根据结构内部的元素对指向结构的指针数组进行排序,c,arrays,sorting,pointers,struct,C,Arrays,Sorting,Pointers,Struct,这里我想做的是根据结构中的字符串对指向结构的指针数组进行排序。我认为只交换数组中的指针而不是交换整个结构是很容易的。所有元素都是动态分配的。我的代码是用德语写的,所以我只会用英语写我不懂的部分。提前谢谢你我感谢你的帮助 typedef struct Vehicle { char *manufacturer; char *serialnumber; int weight; }; void SortByID(struct fahrzeug** data, int Anzahl

这里我想做的是根据结构中的字符串对指向结构的指针数组进行排序。我认为只交换数组中的指针而不是交换整个结构是很容易的。所有元素都是动态分配的。我的代码是用德语写的,所以我只会用英语写我不懂的部分。提前谢谢你我感谢你的帮助

typedef struct Vehicle {
    char *manufacturer;
    char *serialnumber;
    int weight;
};
void SortByID(struct fahrzeug** data, int Anzahl){ 
    struct Vehicle *temp= (struct Vehicle*)malloc( sizeof(struct Vehicle) );
    int i =0 ;// I think i've written something wrong here ( strcmp)
    while ( i < Anzahl && strcmp(data[i]->serialnumber, data[i+1]->serialnumber) < 0) 
    {
         temp = data[i+1];
         data[i+1] = data[i];
         data[i]=temp ;
         i++;
    }  
    free(temp);
    temp=NULL;
}
int main(){
    int count =0 ;
    struct Vehicle **array = NULL ;
    array=(struct Vehicle**)malloc(  5 * sizeof(struct Vehicle*)  );
    for(int i=0;i<5 ;i++){
       array[i] = (struct Vehicle*)malloc( sizeof(struct Vehicle) ) ;
       count++;
    }
    SortByID (  &array, count ) ; // is this right ?
    return 0; 
}

下面是一个使用
qsort()
的工作示例。它可以按序列号和制造商分类

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

struct Vehicle {
    char *manufacturer;
    char *serialnumber;
    int weight;
};

int cmp_serialnumber(const void *p1, const void *p2)
{
    const struct Vehicle *v1 = *(struct Vehicle * const *)p1;
    const struct Vehicle *v2 = *(struct Vehicle * const *)p2;

    return strcmp(v1->serialnumber, v2->serialnumber);

/*
    // Alternatively You could write this function as this less readable one liner
    return strcmp((*(struct Vehicle * const *)p1)->serialnumber,
                    (*(struct Vehicle * const *)p2)->serialnumber);
*/
}

int cmp_manufacturer(const void *p1, const void *p2)
{
    const struct Vehicle *v1 = *(struct Vehicle * const *)p1;
    const struct Vehicle *v2 = *(struct Vehicle * const *)p2;

    return strcmp(v1->manufacturer, v2->manufacturer);
}

void print_vehicles(struct Vehicle **vehicles, int n) {
    for (int i=0; i<n; i++) {
        printf("%s, %s, %d\n", vehicles[i]->serialnumber,
                vehicles[i]->manufacturer, vehicles[i]->weight);
    }
}


#define N_VEHICLES  5
char *manufacturers[] = {"McLaren", "Ferrari", "Renault", "Mercedes", "Alfa Romeo"};
char *serialnumbers[] = {"SN500", "SN4", "SN8", "SN2", "SN1"};


int main(){
    struct Vehicle **vehicles = NULL;

    vehicles = (struct Vehicle**)malloc(N_VEHICLES * sizeof(struct Vehicle *));

    for(int i=0; i<N_VEHICLES; i++) {
        vehicles[i] = (struct Vehicle *)malloc(sizeof(struct Vehicle));
        vehicles[i]->manufacturer = manufacturers[i];
        vehicles[i]->serialnumber = serialnumbers[i];
        vehicles[i]->weight = 1000;
    }

    printf("Before\n");
    print_vehicles(vehicles, N_VEHICLES);
    printf("\n");

    // sort by serial number
    qsort(vehicles, N_VEHICLES, sizeof(struct Vehicle *), cmp_serialnumber);
    printf("Sorted by serial number\n");
    print_vehicles(vehicles, N_VEHICLES);
    printf("\n");

    // sort by manufacturer
    qsort(vehicles, N_VEHICLES, sizeof(struct Vehicle *), cmp_manufacturer);
    printf("Sorted by manufacturer\n");
    print_vehicles(vehicles, N_VEHICLES);

    return 0;
}
#包括
#包括
#包括
结构车辆{
制造商;
字符*序列号;
整数权重;
};
int cmp_序列号(常数无效*p1,常数无效*p2)
{
结构车辆*v1=*(结构车辆*const*)p1;
常数结构车辆*v2=*(结构车辆*常数*)p2;
返回strcmp(v1->serialnumber,v2->serialnumber);
/*
//或者,您可以将此函数编写为可读性较差的单行程序
返回strcmp((*(结构车辆*const*)p1)->序列号,
(*(结构车辆*常数*)p2)->序列号;
*/
}
内部cmp_制造商(常数无效*p1,常数无效*p2)
{
结构车辆*v1=*(结构车辆*const*)p1;
常数结构车辆*v2=*(结构车辆*常数*)p2;
返回strcmp(v1->制造商,v2->制造商);
}
无效打印车辆(结构车辆**车辆,int n){
对于(int i=0;iSeries编号,
车辆[i]->制造商,车辆[i]->重量);
}
}
#定义N_车辆5
char*制造商[]={“迈凯轮”、“法拉利”、“雷诺”、“梅赛德斯”、“阿尔法罗密欧”};
字符*序列号[]={“SN500”、“SN4”、“SN8”、“SN2”、“SN1”};
int main(){
结构车辆**车辆=空;
车辆=(结构车辆**)malloc(N_车辆*尺寸(结构车辆*);
对于(int i=0;imanufacturer=制造商[i];
车辆[i]->serialnumber=序列号[i];
车辆[i]>重量=1000;
}
printf(“之前”);
打印车辆(车辆、非车辆);
printf(“\n”);
//按序列号排序
qsort(车辆,N_车辆,尺寸(结构车辆*),cmp_序列号);
printf(“按序列号排序\n”);
打印车辆(车辆、非车辆);
printf(“\n”);
//按制造商分类
qsort(车辆,N_车辆,尺寸(结构车辆*),cmp_制造商);
printf(“按制造商排序”);
打印车辆(车辆、非车辆);
返回0;
}

您可以使用,而不是编写自己的排序函数。谢谢,我会尝试。qsort将只交换数组中指针的位置?
qsort()
将交换数组中的数据,无论其类型和大小。在您的情况下,数据是指针,因此这就是将按照比较函数的指示交换的所有数据。这是否可以:
int compare(struct Vehicle*a,struct Vehicle*b){return strcmp(a->serialnumber,b->serialnumber);}void SortByID(结构fahrzeug**data,int-Anzahl){qsort(数据,Anzahl,sizeof(结构fahrzeug*),比较);}
看起来不错。试试看。谢谢。你帮了我很多忙。将qsort与指针指向指针查找表一起使用不是一个好主意。它只会浅层复制指针,而不会复制实际数据。因此,如果在调用qsort之前,你有一个指向特定车辆的指针,该指针现在将指向错误的位置。最好使用实际的2D a“@Lundin:也许你的观点太微妙了,我无法理解,但我还无法归纳出问题所在。我的理解是,
struct Vehicle**vehicles
中的值,即指向车辆的指针,将被洗牌,而不会修改,也不会修改任何引用的数据。如果你持有指向某个点的指针呃到一辆车,你可以看到一个变化,但这不是这里发生的(我想),也不是你描述的(你只提到了一个“指向某辆车的指针”)。你能添加一个答案,说明在这种情况下如何正确使用
qsort()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Vehicle {
    char *manufacturer;
    char *serialnumber;
    int weight;
};

int cmp_serialnumber(const void *p1, const void *p2)
{
    const struct Vehicle *v1 = *(struct Vehicle * const *)p1;
    const struct Vehicle *v2 = *(struct Vehicle * const *)p2;

    return strcmp(v1->serialnumber, v2->serialnumber);

/*
    // Alternatively You could write this function as this less readable one liner
    return strcmp((*(struct Vehicle * const *)p1)->serialnumber,
                    (*(struct Vehicle * const *)p2)->serialnumber);
*/
}

int cmp_manufacturer(const void *p1, const void *p2)
{
    const struct Vehicle *v1 = *(struct Vehicle * const *)p1;
    const struct Vehicle *v2 = *(struct Vehicle * const *)p2;

    return strcmp(v1->manufacturer, v2->manufacturer);
}

void print_vehicles(struct Vehicle **vehicles, int n) {
    for (int i=0; i<n; i++) {
        printf("%s, %s, %d\n", vehicles[i]->serialnumber,
                vehicles[i]->manufacturer, vehicles[i]->weight);
    }
}


#define N_VEHICLES  5
char *manufacturers[] = {"McLaren", "Ferrari", "Renault", "Mercedes", "Alfa Romeo"};
char *serialnumbers[] = {"SN500", "SN4", "SN8", "SN2", "SN1"};


int main(){
    struct Vehicle **vehicles = NULL;

    vehicles = (struct Vehicle**)malloc(N_VEHICLES * sizeof(struct Vehicle *));

    for(int i=0; i<N_VEHICLES; i++) {
        vehicles[i] = (struct Vehicle *)malloc(sizeof(struct Vehicle));
        vehicles[i]->manufacturer = manufacturers[i];
        vehicles[i]->serialnumber = serialnumbers[i];
        vehicles[i]->weight = 1000;
    }

    printf("Before\n");
    print_vehicles(vehicles, N_VEHICLES);
    printf("\n");

    // sort by serial number
    qsort(vehicles, N_VEHICLES, sizeof(struct Vehicle *), cmp_serialnumber);
    printf("Sorted by serial number\n");
    print_vehicles(vehicles, N_VEHICLES);
    printf("\n");

    // sort by manufacturer
    qsort(vehicles, N_VEHICLES, sizeof(struct Vehicle *), cmp_manufacturer);
    printf("Sorted by manufacturer\n");
    print_vehicles(vehicles, N_VEHICLES);

    return 0;
}