简体   繁体   English

如何从结构 function 返回结构?

[英]How to return a struct from a struct function?

//The Last attempt//


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct WordNode {
  char word;
  int line;
  struct WordNode* left;
  struct WordNode* right;
};

struct WordNode createNode(char word, int line) {
  struct WordNode* node=NULL;
  node =malloc(sizeof(struct WordNode));
  node->word = word;
  node->line = line;
  node->left = NULL;
  node->right = NULL;
  return node;
}

struct WordNode insert(struct WordNode* root, char word, int line) {
  if (root==NULL) {
    return createNode(word, line);
  }

  int cmp = strcmp(word, root->word);
  if (cmp == 0) {
    // word already exists in tree, so do nothing
    return root;
  } else if (cmp < 0) {
    root->left = insert(root->left, word, line);
  } else {
    root->right = insert(root->right, word, line);
  }

  return root;
}

int main(int argc, char argv[]) {
  if (argc != 2) {
    printf("Usage: %s <filename>\n", argv[0]);
    return 1;
  }

  char filename = argv[1];
  FILE *file = fopen("D:\TXTFolder\Text1.txt", "r");
  if (file == NULL) {
    perror("Error opening file");
    return 1;
  }

  struct WordNode *root = NULL;

  char line[256];
  int lineNumber = 1;
  while (fgets(line, sizeof(line), file)) {
    char word = strtok(line, " \n\t");
    while (word != NULL) {
      root = insert(root, word, lineNumber);
      word = strtok(NULL, " \n\t");
    }
    lineNumber++;
  }

  fclose(file);

  return 0;
}

This is a program that reads a text file and stores unique words from that text file in a binary search tree in alphabetical order and this program also stores in that same binary search tree the index of lines in which those stored unique words were mentioned in C.这是一个程序,它读取一个文本文件并将该文本文件中的唯一单词按字母顺序存储在二叉搜索树中,并且该程序还在同一二叉搜索树中存储在 C 中提到的那些存储的唯一单词的行索引.

There seem to have errors in the two functions createNode() and insert() because they are both supposed to return a struct but it seems I can't. createNode()insert()这两个函数似乎有错误,因为它们都应该返回一个结构,但我似乎不能。

malloc(sizeof(struct WordNode)) allocates enough memory to store a struct WordNode and returns a pointer to that memory; malloc(sizeof(struct WordNode))分配足够的 memory 来存储struct WordNode并返回指向该 memory 的指针; the address of that memory. You assign that to a struct WordNode* (a pointer to a struct WordNode ), and if you want to return that you need to return a pointer to a struct WordNode . memory 的地址。您将其分配给struct WordNode* (指向struct WordNode的指针),如果要返回它,则需要返回指向struct WordNode的指针。

//              v returns a pointer
struct WordNode *createNode(char word, int line) {
  // malloc returns a pointer
  struct WordNode* node = malloc(sizeof(struct WordNode));

  node->word = word;
  node->line = line;
  node->left = NULL;
  node->right = NULL;

  // this is a pointer
  return node;
}

...and everywhere else you return a pointer to a WordNode. ...在其他任何地方都返回一个指向 WordNode 的指针。

You could return a copy of the struct Node ...您可以返回struct Node的副本...

struct WordNode createNode(char *word, int line) {
  struct WordNode* node=NULL;
  node =malloc(sizeof(struct WordNode));
  node->word = word;
  node->line = line;
  node->left = NULL;
  node->right = NULL;

  //     v dereferences the pointer so it refers to the actual memory
  return *node;
}

...but now you'd can't check things like node == NULL . ...但是现在您无法检查诸如node == NULL

Get used to working with pointers.习惯于使用指针。


Other issues are that your "word" is a single character, a char , but you're treating it as a string.其他问题是您的“单词”是单个字符char ,但您将其视为字符串。 Strings in C are a pointer to an array of characters, a char * . C 中的字符串是指向字符数组的指针,即char *

struct WordNode {
  //   v pointer
  char *word;
  int line;
  struct WordNode* left;
  struct WordNode* right;
};

...and everywhere else you use word needs to be a char *word . ...以及您在其他任何地方使用word都必须是char *word

You also need to declare argv[] as an array of character pointers (strings).您还需要将 argv[] 声明为字符指针(字符串)数组。

//                      v array of strings
int main(int argc, char *argv[]) {

strtok does not copy the word, it simply points to the word in the original string. strtok不复制单词,它只是指向原始字符串中的单词。 When you store the word returned from strtok , it is pointing to memory in line .当您存储从strtok返回的word时,它指向 memory line line is overwritten each time you read a line, so the stored words will be overwritten.每读line都会覆盖一行,因此存储的单词将被覆盖。

You need to copy the word to new memory. Use strdup .您需要将单词复制到新的 memory。使用strdup

  // line's memory is overwritten each time fgets is called
  while (fgets(line, sizeof(line), file)) {
    // word points to memory in line
    char word = strtok(line, " \n\t");
    while (word != NULL) {
      //                  vvvvvvvvvvvv copy the word from line to new memory
      root = insert(root, strdup(word), lineNumber);
      // word points to memory in line
      word = strtok(NULL, " \n\t");
    }
    lineNumber++;
  }

Backslashes in double-quoted strings indicate the following character has special meaning, like \n for a newline and \t for a tab.双引号字符串中的反斜杠表示后面的字符具有特殊含义,例如\n表示换行符和\t表示制表符。 If you mean a literal backslash, use \\ .如果您指的是文字反斜杠,请使用\\

  FILE *file = fopen("D:\\TXTFolder\\Text1.txt", "r");

to return a struct from a struct function?从结构 function 返回一个结构?

Simply return the struct like code would return an int .简单地返回struct就像代码会返回一个int一样。

struct WordNode createNode(char word, int line) {
  //struct WordNode* node=NULL;
  //node =malloc(sizeof(struct WordNode));
  //node->word = word;
  //node->line = line;
  //node->left = NULL;
  //node->right = NULL;
  struct WordNode node = {.word = word, .line = line, .left = NULL, .roght = NULL};
  return node;
}

This is not a good idea though.但这不是一个好主意。 Better to allocate memory and return a pointer to the struct WordNode .最好分配 memory 并返回指向struct WordNode的指针。

Very few words can be saved in a char . char中可以保存很少的单词 A word like 'A' , 'I' , 'O' so far more likely the function should accept a pointer to characters ( char * ) and the createNode() would allocate a copy of that referenced string .'A''I''O'这样的词更有可能 function 应该接受指向字符 ( char * ) 的指针,并且createNode()将分配该引用字符串的副本。

For my_strdup() , see this .对于my_strdup() ,请参阅

struct WordNode *createNode(const char *word, int line) {
  struct WordNode* node = malloc(sizeof node[0]);
  if (node) {
    node->word = my_strdup(word);
    node->line = line;
    node->left = NULL;
    node->right = NULL;
  }
  return node;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM