如何对动态分配的二维字符数组进行qsort

发布于 2024-12-17 05:30:48 字数 896 浏览 0 评论 0原文

我试图对动态分配的二维数组进行排序,但没有成功。我认为它与动态分配有关,因为如果我使用全局二维数组(例如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等风来 2024-12-24 05:30:48

您的 char filelist[4095][100] 示例与 allocateFileListArray 中的分配不同,指针数组与数组的数组不同。
您应该像这样更改 qsort 的比较函数

int compareStrings(const void *stackA, const void *stackB)
{
  const char *a=*(const char**)stackA;
  const char *b=*(const char**)stackB;
  return strcmp(a,b);
}

,并且可以使用该函数而无需在 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

int compareStrings(const void *stackA, const void *stackB)
{
  const char *a=*(const char**)stackA;
  const char *b=*(const char**)stackB;
  return strcmp(a,b);
}

and you can use this function without ugly cast in qsort.

只为守护你 2024-12-24 05:30:48

怎么样:

void sortTheArray(void)
{
    qsort (fileList, arraySize, sizeof(char *), (compfn) compareStrings );
}

查看 qsort 的定义并相应地传递参数。

如果这不起作用,请查看compareStrings()。您的一级间接性是否过多或过少?因为没有关于compareStrings()的信息,所以不可能说它是否与它有关。

How about:

void sortTheArray(void)
{
    qsort (fileList, arraySize, sizeof(char *), (compfn) compareStrings );
}

?

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.

森林迷了鹿 2024-12-24 05:30:48

我对二维分配的 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]).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文