c - 按特定字段对二维字符串数组进行排序

标签 c arrays sorting multidimensional-array qsort

给定下面贴花的二维数组:

char * Arr[4] = 
{
    {"124 -346  DATA...."},
    {"39479 -32 MOREDATA...."},
    {"12 -1 DATA2...."},
    {"100 -45 DATA4...."}
};

我正在尝试使用 qsort()根据 SECOND 字段对该函数进行排序,这意味着字符串将根据最低的第二个值(-1、-32、-45、-346)排序。如果每个值只有一位,我知道如何实现此功能,但程序中的数字可以任意长。这是我所拥有的,但程序崩溃了,如果有更有效的方法来对这些数据进行排序,我会喜欢这里(我知道我的方法不是很有效)。

排序函数(qsort() 调用):

inline void GetStr(char *ix, char* Result)  //call to get second number in function
{
    char *spacing;              //iterator to traverse
    spacing = ix;               //iterator = pos of ix
    int LEN = 0;                //length and offset
    int Offset = 0;

    while(*spacing != ' ')      //while not at end of first num
    {
        Offset++;               //offset is more
        spacing++;
    }
    spacing++;                  //go one ahead of the space
    Offset++;

    while(*spacing != ' ')      //while not end of second number
    {
        spacing++;
        Offset++;
        LEN++;                  //length of number
    }
    strncpy(Result, ix + (Offset - LEN),LEN);
}

int sort(const void* a, const void* b)
{
    char *ia = *(char**)a;
    char *ib = *(char**)b;

    char * Str;
    char * Str2;
    GetStr(ia, Str);                                    //getting some strange errors....... program just crashes
    GetStr(ib, Str2);
    printf("Str: %s Str2: %s", Str, Str2);
    int n1 = atoi(Str);
    int n2 = atoi(Str2);
    return (n1 > n2);
}

最佳答案

我相信你至少有一个问题:

strncpy(Result, ix + (Offset - LEN),LEN);

如果您查看 documentation for strncpy ,您将看到,如果达到字符数限制,它不会自动以 null 终止复制的字符串。因此你的Result字符串不是空终止的。

试着改成这样:

strncpy(Result, ix + (Offset - LEN),LEN);
Result[LEN] = '\0';

当然,你还是需要为Result字符串提供内存。当前您正在将未初始化的指针传递给 GetStr() :

char * Str;
char * Str2;

由于这些是相当小的整数,您可以像这样使用静态分配的存储空间:

#define MAX_RESULT_LEN 64

/* ... */

char Str[MAX_RESULT_LEN]
char Str2[MAX_RESULT_LEN]

/* ... */

if (LEN > MAX_RESULT_LEN - 1) {
    LEN = MAX_RESULT_LEN - 1;
}

strncpy(Result, ix + (Offset - LEN),LEN);
Result[LEN] = '\0';

最后,您的 sort() 存在一些问题功能。如果您查看 qsort() documentaton ,你可以看到返回值应该是“一个小于、等于或大于零的整数,如果第一个参数被认为分别小于、等于或大于第二个”。实现此目的的最简单方法是使用逻辑 n1 - n2而不是 n1 < n2 .

我还以为你在排序 char ** 类型的参数也很奇怪,但经过进一步思考,我意识到它们是正确的。来自 qsort 文档:“指向被比较对象的两个参数”。所以它们确实是指向 C 字符串或 char ** 的指针.

所以这是最终版本:

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

#define MAX_RESULT_LEN 64

void GetStr(char *ix, char* Result)  //call to get second number in function
{
    char *spacing;              //iterator to traverse
    spacing = ix;               //iterator = pos of ix
    int LEN = 0;                //length and offset
    int Offset = 0;

    while(*spacing != ' ')      //while not at end of first num
    {
        Offset++;               //offset is more
        spacing++;
    }
    spacing++;                  //go one ahead of the space
    Offset++;

    while(*spacing != ' ')      //while not end of second number
    {
        spacing++;
        Offset++;
        LEN++;                  //length of number
    }

    if (LEN > MAX_RESULT_LEN - 1) {
        LEN = MAX_RESULT_LEN - 1;
    }

    strncpy(Result, ix + (Offset - LEN),LEN);
    Result[LEN] = '\0';
}

int sort(const void* a, const void* b)
{
    char *ia = *(char **)a;
    char *ib = *(char **)b;

    char Str[MAX_RESULT_LEN];
    char Str2[MAX_RESULT_LEN];

    GetStr(ia, Str);
    GetStr(ib, Str2);

    printf("Str: %s Str2: %s", Str, Str2);
    int n1 = atoi(Str);
    int n2 = atoi(Str2);
    return (n1 - n2);
}

int main(void) {
    char * Arr[4] = 
    {
        {"124 -346  DATA...."},
        {"39479 -32 MOREDATA...."},
        {"12 -1 DATA2...."},
        {"100 -45 DATA4...."}
    };

    qsort(Arr, 4, sizeof(char *), sort);
}

关于c - 按特定字段对二维字符串数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13595662/

相关文章:

linux - awk 第一列日期的第二列最大值

c - 从文件中扫描字符时出现问题

c - 简易蹦床 Hook

arrays - 将 ls 输出转换为数组

JavaScript:使用 Join 使数组成为字符串,使用输入参数作为分隔符 Btw Words --> Replace Error

java - 如何从 ArrayList 交换两个子列表?

c - 如何使用c查找字符串中的文章数?

c - 使用fread()函数读取txt文件

javascript - 如何删除javascript数组中的空白元素?

java - Java 8 中的自定义排序映射