qsort 不会对动态分配的结构数组进行排序 [英] qsort won't sort dynamically allocated array of structs

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

问题描述

我有一个结构

struct info {
    char firstName[100]; 
    char lastName[100]; 
    char companyName[100];
    char email[100];
    unsigned long phoneNumber; 
}; 

存储在文件 compareElements.h

which is stored in the file compareElements.h

我将一组值读入一个动态分配的结构数组,称为 bptr.

I read in a set of values into a dynamically allocated array of structs called bptr.

我的 comparePtr 指向这个函数.

my comparePtr points to this function.

#include <string.h>
#include "compareElements.h"
int compareNameAscending (const void *a, const void *b) {

    struct info *part1 = (struct info *) a; 
    struct info *part2 = (struct info *) b; 




    if(part1 -> lastName[0] != '\0' && part2 -> lastName[0] != '\0') {
        return (strcmp(part1 -> lastName , part2 -> lastName));
    }   
    if (part1 -> lastName[0] == '\0' && part2 -> lastName[0] != '\0') {
        return (strcmp(part1 -> companyName , part2 -> lastName)); 
    }

    if (part1 -> lastName[0] != '\0' && part2 -> lastName[0] == '\0') {
        return (strcmp(part1 -> lastName, part2 -> companyName)); 
    }

    if (part1 -> lastName[0] == '\0' && part2 -> lastName[0] == '\0') {
        return (strcmp(part1 -> companyName, part2 -> companyName)); 
    } 

}

在主函数中我有

comparePtr = &compareNameAscending 

问题是并非所有结构都需要拥有 lastName 和 companyName 两者中的一个.所以我想按姓氏排序,如果他们没有姓氏,我会按公司名称排序.问题是我的 qsort 调用实际上并没有改变结构的顺序,它只是吐出原始结构.这是 qsort 调用.

The problem is that not all structs need to have a lastName and companyName only one of the two. So I want to sort by last name, and if they don't have a last name I'll sort by company name. The problem is that my qsort call doesn't ever actually change the order of the struct it just spits out the original. This is the qsort call.

qsort(bptr, i, sizeof(struct info), comparePtr); 

推荐答案

我想你的代码初始化有问题,不是如何使用它,这里是您的代码的简化版本,可以正常工作:

I suppose you have problem in initialization of your code, not how to use it, here simplified version of your code that works fine:

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

struct info {
    char lastName[100]; 
    char companyName[100];
}; 

int compareNameAscending (const void *a, const void *b) {

    const struct info *part1 = a; 
    const struct info *part2 = b; 

    const char *s1 = strcmp(part1->lastName, "") == 0 ?
        part1->companyName : part1->lastName;
    const char *s2 = strcmp(part2->lastName, "") == 0 ?
        part2->companyName : part2->lastName;

    return strcmp(s1, s2);
}


int main() {
    struct info bptr[2];
    strcpy(bptr[0].lastName, "");
    strcpy(bptr[0].companyName, "Foo");
    strcpy(bptr[1].lastName, "Boo");
    strcpy(bptr[1].companyName, "");
    qsort(bptr, sizeof(bptr) / sizeof(bptr[0]), sizeof(struct info), compareNameAscending);

    size_t i;
    for (i = 0; i < sizeof(bptr) / sizeof(bptr[0]); ++i) {
        printf("company %s, name %s\n", bptr[i].companyName, bptr[i].lastName);
    }
}

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

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