【发布时间】:2021-11-27 16:49:17
【问题描述】:
我正因为这个问题把头撞到墙上。
总结一下: 我需要动态地将字符串添加到数组中,对它们进行排序,然后检查另一个字符串值。
这需要在支持 C 作为脚本语言但功能有限的 SCADA 系统上工作。我有 qsort() 可用。
但是,使用我拥有的测试代码,我无法在数组上使用 qsort,其值是动态添加的。
为了清楚起见,我可以将字符串添加到数组中,效果很好。 但是,当我在该数组上调用 qsort() 时,我无法再打印出索引。
这是目前的代码(请客气,我对 C 语言不是很精通):
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int cstring_cmp (const void *a, const void *b)
{
// This function is taken from an online example
const char **ia = (const char **) a;
const char **ib = (const char **) b;
return strcmp (*ia, *ib);
}
int main ()
{
//char *ArchiveKomponents[] = {"R1890L", "F1121D", "F1284Z", "A1238K"};
// If I do the above commented out, it works as intended
char ArchiveKomponents[100][20];
strcpy(ArchiveKomponents[0], "R1890L");
strcpy(ArchiveKomponents[1], "F1284Z");
size_t strLen = sizeof (ArchiveKomponents) / sizeof (char *);
printf ("Len: %zu\n", strLen);
printf ("Before [0]: %s\n", ArchiveKomponents[0]);
printf ("Before [1]: %s\n", ArchiveKomponents[1]);
qsort (ArchiveKomponents, (size_t)strLen, sizeof (char *), cstring_cmp);
printf ("After [0]: %s\n", ArchiveKomponents[0]);
printf ("After [1]: %s\n", ArchiveKomponents[1]);
// When run, the "After" prints are not even printed, the program simply halts
return 0;
}
我觉得我已经用谷歌搜索了整个互联网,以寻找有关如何做到这一点的答案,但没有运气。
问候
【问题讨论】:
-
你需要比较
char[20]而不是char*。 -
为什么要对整个数组进行排序?你只初始化了两个元素!
-
@MartinJames 我本来可以更清楚的,我的错。在这个测试中,我只尝试了 2 个元素,但最后我可能需要使用多达 100 个元素,因此数组的大小为 100。在数组声明上方的注释行中,您将看到 4 个元素
标签: arrays c string qsort wincc