簡體   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;
}

這是一個程序,它讀取一個文本文件並將該文本文件中的唯一單詞按字母順序存儲在二叉搜索樹中,並且該程序還在同一二叉搜索樹中存儲在 C 中提到的那些存儲的唯一單詞的行索引.

createNode()insert()這兩個函數似乎有錯誤,因為它們都應該返回一個結構,但我似乎不能。

malloc(sizeof(struct WordNode))分配足夠的 memory 來存儲struct WordNode並返回指向該 memory 的指針; 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;
}

...在其他任何地方都返回一個指向 WordNode 的指針。

您可以返回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;
}

...但是現在您無法檢查諸如node == NULL

習慣於使用指針。


其他問題是您的“單詞”是單個字符char ,但您將其視為字符串。 C 中的字符串是指向字符數組的指針,即char *

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

...以及您在其他任何地方使用word都必須是char *word

您還需要將 argv[] 聲明為字符指針(字符串)數組。

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

strtok不復制單詞,它只是指向原始字符串中的單詞。 當您存儲從strtok返回的word時,它指向 memory line 每讀line都會覆蓋一行,因此存儲的單詞將被覆蓋。

您需要將單詞復制到新的 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++;
  }

雙引號字符串中的反斜杠表示后面的字符具有特殊含義,例如\n表示換行符和\t表示制表符。 如果您指的是文字反斜杠,請使用\\

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

從結構 function 返回一個結構?

簡單地返回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;
}

但這不是一個好主意。 最好分配 memory 並返回指向struct WordNode的指針。

char中可以保存很少的單詞 'A''I''O'這樣的詞更有可能 function 應該接受指向字符 ( char * ) 的指針,並且createNode()將分配該引用字符串的副本。

對於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