使用STDLIB的的qsort()排序字符串数组 [英] Using stdlib's qsort() to sort an array of strings
问题描述
有些preface:我是一名计算机工程专业的学生采取用C一流的Java之后的3个学期(最高数据结构)。这个问题是关于一个家庭作业,但几步从解决它为我删除。
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.
我有读入存储器,使得它被存储在字符的输入文件[9] [500]。我最大长度为500最具串读8.我试图使用排序STDLIB的内置的qsort()函数这个数组,和我有一些内存错误。
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.
code的重要片段:
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++;
}
本部分读取数据到阵列。在这一部分感兴趣的数组是调试。这是上面指定的阵列。这是我比较函数的qsort为:
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);
}
这是我试图调用的qsort:
This is my attempt to call qsort:
size_t debug_len = sizeof(debug)/sizeof(char*);
printf("debug len: %d, count: %d, sizeof(char*): %d\n",debug_len,count,sizeof(char*));
qsort(debug,count, sizeof(char *), compare);
我试图在我的电话代debug_len其中count是的,但我仍然段错误。下面是输出:
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)
感谢您!
推荐答案
比较功能将接收指针所比较的元素。你实际上试图比较使用 STRNCMP字符()
。既然你有指向每个字符串,将它转换为一个的char *
和比较。
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);
}
也请记住,这是一个数组的数组,而不是指针数组。因此,一个元素的大小应该是数组的大小, 9
,而不是指针, 4
。在这一点上,它会更容易只使用 sizeof的调试[0]
,因为它是一个二维数组。如果你没有用正确的尺寸做到这一点,的qsort()
只会破坏你的数组。
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屋!