c - Qsort 字符串结构

标签 c string struct qsort

请问你能帮我对字符串结构进行排序吗? 我需要按字典中的字母顺序对单词进行排序。问题是,它给我带来了段错误...... 这是我的结构:

typedef struct {
    int length;
    char *data;
} Word;

typedef struct {
    int length;
    int index;
    Word *data;
} Dictionary;

这是比较函数:

int compare(const void *a, const void *b) 
{ 
    return strcmp (((Word *)a)->data, ((Word *)b)->data);
} 

这是 qsort 实现:

qsort(&dictionary, dictionary.index, sizeof (Word *), compare); 

非常感谢您的帮助。

最佳答案

您正在尝试qsort您的字典结构而不是字典数据。

qsort(dictionary.data, dictionary.index, sizeof (Word *), compare); 

您还应该仔细检查以确保 dictionary.index 是字典的长度,而不是 dictionary.length

关于c - Qsort 字符串结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27473459/

相关文章:

c - 将 sizeof 与具有固定长度数组的结构一起使用

c - 在 C 好奇心中包含头文件

将 float 转换为 int 会改变值

C 对传入字符串的字符串操作

c - 从 C 手动为 excel 创建多表文件

C 的 strtok() 和只读字符串文字

javascript - 将字符串转换为嵌套的 JavaScript 对象或 JSON

string - 在JavaFX 2中计算单行文本尺寸

C 程序 : Reading from file and copying to a struct?

c++ - 从链表中返回一些东西