使用 stdlib 的 qsort() 对字符串数组进行排序 [英] Using stdlib's qsort() to sort an array of strings
问题描述
一些前言:我是一名计算机工程专业的学生,在学习了 3 个学期的 Java(直至数据结构)后,我正在上 C 的第一堂课.这个问题与家庭作业有关,但离我解决它还有几个步骤.
我有一个读入内存的输入文件,它存储在 char[9][500] 中.我最多读入 500 个最大长度为 8 的字符串.我正在尝试使用 stdlib 内置的 qsort() 函数对该数组进行排序,但出现了一些内存错误.
重要的代码片段:
字符数据[4][500][60];字符调试[500][9];size_t 计数 = 0;/* 初始化文件,打开读取 */文件* p用户日志;pUserlog = fopen("userlog","r");而(!feof(pUserlog)){fscanf(pUserlog, "%9s %8s %16s",debug[count], data[1][count], data[2][count]);fgets(data[3][count], 60, pUserlog);计数++;}
本节将数据读入数组.这部分感兴趣的数组是调试".这是上面指定的数组.这是我对 qsort 的比较函数:
int compare(const void* a, const void* b){常量字符 **ia = (常量字符 **)a;const char **ib = (const char **)b;puts("我在比较!");返回 strncmp(*ia, *ib,8);}
这是我尝试调用 qsort:
size_t debug_len = sizeof(debug)/sizeof(char*);printf("debug len: %d, count: %d, sizeof(char*): %d
",debug_len,count,sizeof(char*));qsort(调试,计数,sizeof(char *),比较);
我尝试在 count 所在的调用中替换 debug_len,但仍然存在段错误.这是输出:
<上一页>$ ./测试调试长度:1125,计数:453,sizeof(char*):4我在比较!分段错误(核心转储)谢谢!
比较函数将接收指向正在比较的元素的指针.您正在有效地尝试使用 strncmp()
比较字符.由于您有指向每个字符串的指针,因此将其转换为 char *
并进行比较.
int compare(const void* a, const void* b){常量字符 *ia = (常量字符 *)a;常量字符 *ib = (常量字符 *)b;puts("我在比较!");返回 strncmp(ia, ib, 9);}
还要记住,它是一个数组数组,而不是一个指针数组.所以元素的大小应该是数组的大小,9
,而不是指针的大小,4
.此时,只使用 sizeof debug[0]
会更容易,因为它是一个二维数组.如果您不使用正确的大小执行此操作,qsort()
只会破坏您的数组.
size_t elemsize = sizeof debug[0];/* 9 - 每个元素的大小 */size_t 计数 = sizeof(debug)/elemsize;/* 500 - 数组元素个数 */qsort(调试,计数,elemsize,比较);
Some preface: I'm a computer engineering student taking a first class in C after 3 semesters of Java (up to data structures). This question is in relation to a homework assignment, but a few steps removed from solving it for me.
I have an input file that I read into memory such that it is stored in char[9][500]. I read in at most 500 strings of maximum length 8. I am attempting to sort this array using stdlib's built in qsort() function, and am having some memory errors.
Important snippets of code:
char data[4][500][60];
char debug[500][9];
size_t count = 0;
/* initialize file, open for reading */
FILE* pUserlog;
pUserlog = fopen("userlog","r");
while(!feof(pUserlog))
{
fscanf(pUserlog, "%9s %8s %16s",debug[count], data[1][count], data[2][count]);
fgets(data[3][count], 60, pUserlog);
count++;
}
This section reads the data into the arrays. The array of interest in this part is "debug". This is the array specified above. Here is my comparison function for qsort:
int compare(const void* a, const void* b)
{
const char **ia = (const char **)a;
const char **ib = (const char **)b;
puts("I'm in compare!");
return strncmp(*ia, *ib,8);
}
This is my attempt to call qsort:
size_t debug_len = sizeof(debug)/sizeof(char*);
printf("debug len: %d, count: %d, sizeof(char*): %d
",debug_len,count,sizeof(char*));
qsort(debug,count, sizeof(char *), compare);
I attempted substituting debug_len in my call where count is, but I am still segfaulting. Here is the output:
$ ./test debug len: 1125, count: 453, sizeof(char*): 4 I'm in compare! Segmentation fault (core dumped)
Thank you!
The compare function will receive pointers to the elements that are being compared. You are effectively trying to compare characters using strncmp()
. Since you have pointers to each of the strings, cast it to a char *
and compare.
int compare(const void* a, const void* b)
{
const char *ia = (const char *)a;
const char *ib = (const char *)b;
puts("I'm in compare!");
return strncmp(ia, ib, 9);
}
Remember also, it's an array of arrays, not an array of pointers. So the size of an element should be the size of the array, 9
and not of the pointer, 4
. At this point, it would be easier to just use sizeof debug[0]
since it is a two-dimensional array. If you don't do this with the right sizes, qsort()
will just destroy your array.
size_t elemsize = sizeof debug[0]; /* 9 - size of each element */
size_t count = sizeof(debug)/elemsize; /* 500 - number of elements in array */
qsort(debug, count, elemsize, compare);
这篇关于使用 stdlib 的 qsort() 对字符串数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!