指针排序数组结构的qsort [英] sort array of pointers to structures qsort

查看:350
本文介绍了指针排序数组结构的qsort的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想排序的指针数组结构,其中的关键,就是比较结构的财产之一。

我认为这可能是比较的方法。

下面是一个例子code。

 的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;结构BINARY_ARRAY_RECORD {
    字符*名称;
};INT比较(常量无效*节点1,常量无效*节点2){
    返回STRCMP(
        ((结构BINARY_ARRAY_RECORD *)节点1) - >的名字,
        ((结构BINARY_ARRAY_RECORD *)节点2) - GT;名
    );
}诠释的main()
{
    结构BINARY_ARRAY_RECORD **记录;    记录=的malloc(sizeof的(结构BINARY_ARRAY_RECORD *)* 2);    记录[0] =的malloc(sizeof的(结构BINARY_ARRAY_RECORD));
    记录[1] =的malloc(sizeof的(结构BINARY_ARRAY_RECORD));    记录[0] - >名称=的malloc(sizeof的(字符)*(的strlen(字符串2)+ 1));
    记录[1] - >名称=的malloc(sizeof的(字符)*(的strlen(字符串1)+ 1));    的strcpy(记录[0] - GT;名称,字符串2);
    的strcpy(记录[1] - >名称,字符串1);    的qsort(记录,2,sizeof的(记录[0]),比较);    的printf(%S \\ n,记录[0] - >名);
    的printf(%S \\ n,纪录[1] - >名);    返回0;
}


解决方案

我想这应该是简单的。

  INT比较(常量无效*节点1,常量无效*节点2){
      BINARY_ARRAY_RECORD * ptr1的= *(BINARY_ARRAY_RECORD * const的*)节点1;
      BINARY_ARRAY_RECORD * PTR2 = *(BINARY_ARRAY_RECORD * const的*)节点2;
      返回STRCMP(ptr1->的名字,ptr2->名);
    }

和也是我认为的qsort函数调用可以肯定的权利,如果它是这样的事情,

 的qsort(记录,2,sizeof的(BINARY_ARRAY_RECORD *),比较);

我觉得第三个参数必须是结构的大小,你可以绝对肯定,如果它是像上面..

I'm trying to sort an array of pointers to structures where the key to compare is one of the structure's property.

I think that probably is the compare method.

Here's an example code.

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

struct BINARY_ARRAY_RECORD {
    char *name;
};

int compare(const void *node1, const void *node2) {
    return strcmp(
        ((struct BINARY_ARRAY_RECORD *) node1)->name,
        ((struct BINARY_ARRAY_RECORD *) node2)->name
    );
}

int main()
{
    struct BINARY_ARRAY_RECORD **records;

    records = malloc(sizeof(struct BINARY_ARRAY_RECORD *) * 2);

    records[0] = malloc(sizeof(struct BINARY_ARRAY_RECORD));
    records[1] = malloc(sizeof(struct BINARY_ARRAY_RECORD));

    records[0]->name = malloc(sizeof(char) * (strlen("string2") + 1));
    records[1]->name = malloc(sizeof(char) * (strlen("string1") + 1));

    strcpy(records[0]->name, "string2");
    strcpy(records[1]->name, "string1");

    qsort(records, 2, sizeof(records[0]), compare);

    printf("%s\n", records[0]->name);
    printf("%s\n", records[1]->name);

    return 0;
}

解决方案

I guess this should be simpler..

  int compare(const void *node1, const void *node2) {
      BINARY_ARRAY_RECORD *ptr1 = *(BINARY_ARRAY_RECORD * const *)node1;
      BINARY_ARRAY_RECORD *ptr2 = *(BINARY_ARRAY_RECORD * const *)node2;
      return strcmp(ptr1->name, ptr2->name);
    }

And also I think the qsort function call could be definitely right if it were something like this,

qsort(records, 2, sizeof(BINARY_ARRAY_RECORD*), compare);

I think the third argument must be the size of the structure and you can be definitely be sure if it were something like the above..

这篇关于指针排序数组结构的qsort的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆