【问题标题】:Sort char* in structs - getting garbage对结构中的 char* 进行排序 - 获取垃圾
【发布时间】:2015-06-27 05:12:30
【问题描述】:

我创建了一个节点结构数组,并尝试根据节点的 char* 变量“word”按字母顺序对节点进行排序。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "memwatch.h"
#include "concord.h"

#define BUFFSIZE 1000

int main(int argc, char** argv)
{
  Node** list;
  printf("%s%s\n","The file name is ",  argv[1]);
  readInputFile(argv[1], list);
  return 0;
}

int compareWords(const void* nodeA, const void* nodeB)
{
  Node* nodeAA = (Node *) nodeA;
  Node* nodeBB = (Node *) nodeB;
  puts("now here\n");
  printf("%s\n", nodeAA->word);
  printf("%s\n", nodeBB->word);
  return strcmp(nodeAA->word, nodeBB->word);
}

void readInputFile(char* filename, Node** wordList)
{
  FILE* file;
  file = fopen(filename, "r");
  wordList = calloc(BUFFSIZE, sizeof(Node*));

  char* currentWord;
  currentWord = (char*) malloc(sizeof(char) *BUFFSIZE);
  int i;
  i = 0;
  while(fscanf(file, "%s", currentWord)  == 1)
  {
    wordList[i] = (Node*) malloc(sizeof(Node));
    wordList[i]->word = strdup(currentWord);
    puts(wordList[i]->word);
  }
  fclose(file);
  qsort(wordList, i, sizeof(Node), compareWords);
}

在我尝试打印比较函数中的单词时打印出垃圾之前,现在看起来该函数甚至没有被调用。

【问题讨论】:

  • 一般要Node* list;,然后是readInputFile(argv[1], &amp;list);
  • 参数wordList在out参数中,所以正确初始化*wordList=calloc(...。

标签: c struct char


【解决方案1】:

现在看起来该函数甚至没有被调用。

这是因为要对 0 元素列表进行排序,您永远不需要比较两个元素:

  // ...
  int i;
  i = 0;    // --- set to 0
  while(fscanf(file, "%s", currentWord)  == 1)
  {
    // i not changed ... causes other problems, too
    // (contents omited)
  }
  fclose(file);
  // i is still 0
  qsort(wordList, i, sizeof(Node), compareWords);
  // ...

除此之外,正如 David C. Rankin 在评论中指出的那样,您对“输出参数”的使用是错误的。在这种情况下,我还建议只使用返回值。

此外,我将该函数拆分为多个函数:

// Does the file opening and closing, calls readInput
Node * readInputFile(char const *);
// The actual reading
Node * readInput(FILE *)
// Probably do the sorting outside of these functions

【讨论】:

    【解决方案2】:

    [首先你的问题是不完整的,因为它没有向我们展示Node的定义。]

    但是,这里有三个问题:

    1. 我做了一个节点结构数组

      你没有。

      这里

      wordList = calloc(BUFFSIZE, sizeof(Node*));
      

      您为Node 的指针 数组分配内存。

      然后在这里

      wordList[i] = (Node*) malloc(sizeof(Node));
      

      您为之前创建的指针数组的每个元素分配了一块单独的内存。

      后者可能分散在整个进程的内存中。它们不会像qsort() 预期的那样位于连续的内存块中,这可能是以下原因:

      [之前]我打印出垃圾

    2. 从readInputFile() 返回时,wordList 的值丢失。

    3. 读取循环不会增加索引计数器i。


    修复 1. 和 2. 创建一个数组并将对它的引用返回给 readInputFile() 的调用者,就像这样

    *wordList = calloc(BUFFSIZE, sizeof **wordList);
    

    然后像这样调用qsort():

    qsort(*wordList, i, sizeof(Node), compareWords);
    

    修复 3. 这样做:

    size_t i = 0; /* No need for negative indexes here .*/
    while((i < BUFFSIZE) /* Male sure not to overflow the array. */   
          && (fscanf(file, "%s", currentWord) == 1))
    {
      (*wordList)[i].word = strdup(currentWord); /* This is POSIX not Standard C. */
      puts((*wordList)[i].word);
      ++i;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多