如何对动态分配的二维字符数组进行qsort
我试图对动态分配的二维数组进行排序,但没有成功。我认为它与动态分配有关,因为如果我使用全局二维数组(例如 fileList[148096][100]),事情就可以正常工作。有什么想法吗?
// global:
char **fileList;
void allocateFileListArray(void)
{
int arraySize = 148096;
int fileNameLength = 100;
/* allocate storage for an array of pointers */
fileList = (char **) malloc(arraySize * sizeof(char *));
/* for each pointer, allocate storage for an array of chars */
for (int i = 0; i < arraySize; i++)
{
if ((fileList[i] = (char *) malloc(fileNameLength * sizeof(char))) == NULL)
printf("failed fileList alloc\n");
}
}
void sortTheArray(int fileListCount)
{
qsort (( char * ) fileList, fileListCount, sizeof ( *fileList ),
(compfn) compareStrings );
}
int compareStrings(char *stackA, char *stackB)
{
int result;
result = strcmp( stackA->name, stackB->name);
return(result);
}
I am trying to qsort a dynamically-allocated 2d array, without success. I assume it has something to do with the dynamic allocation, because things work fine if I use a global 2d array (eg, fileList[148096][100]). Any ideas?
// global:
char **fileList;
void allocateFileListArray(void)
{
int arraySize = 148096;
int fileNameLength = 100;
/* allocate storage for an array of pointers */
fileList = (char **) malloc(arraySize * sizeof(char *));
/* for each pointer, allocate storage for an array of chars */
for (int i = 0; i < arraySize; i++)
{
if ((fileList[i] = (char *) malloc(fileNameLength * sizeof(char))) == NULL)
printf("failed fileList alloc\n");
}
}
void sortTheArray(int fileListCount)
{
qsort (( char * ) fileList, fileListCount, sizeof ( *fileList ),
(compfn) compareStrings );
}
int compareStrings(char *stackA, char *stackB)
{
int result;
result = strcmp( stackA->name, stackB->name);
return(result);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 char filelist[4095][100] 示例与 allocateFileListArray 中的分配不同,指针数组与数组的数组不同。
您应该像这样更改 qsort 的比较函数
,并且可以使用该函数而无需在 qsort 中进行丑陋的转换。
Your char filelist[4095][100] example is not the same as your allocation in allocateFileListArray, an array of pointers is not the same as an array of an array.
You should change the compare function for qsort like
and you can use this function without ugly cast in qsort.
怎么样:
?
查看 qsort 的定义并相应地传递参数。
如果这不起作用,请查看compareStrings()。您的一级间接性是否过多或过少?因为没有关于compareStrings()的信息,所以不可能说它是否与它有关。
How about:
?
Have a look a the definition of qsort and pass the parameters accordingly.
If that does not work, have a look at compareStrings(). Do you have one level of indirection too much or too less? Because there is no information about compareStrings(), it is impossible to say, if it has something to do with it.
我对二维分配的 int 数组也有类似的问题。这是指向已分配空间的指针数组。有几个棘手的组件:
1)比较函数中的指针转换,正如@user411313提到的,它是一个指向指针的指针,因此您必须取消引用两次才能获取值
2)传递给qsort的大小。大小是指针的大小,而不是二维数组的列数(如 var[row][col] 的情况)。
I had a similar problem with 2D allocated int arrays. That is an array of pointers to malloced space. There are a couple tricky components:
1) pointer casting in the compare function, as @user411313 mentioned, it's a pointer to a pointer so you must dereference twice to get the value
2) the size you pass to qsort. The size is the size of a pointer, and not the column count of the 2D array (as is the case with var[row][col]).