c - 尝试用 C 创建基本的二叉搜索树,但对 'unused variable' 感到困惑?

标签 c binary-search-tree unused-variables

创建了一个基本的二叉搜索树,利用链表并尝试将“数据”输入到函数中(因为它需要它)。 但是,即使我正在使用它,我仍然收到“未使用的变量”错误?

是因为我没有返回“数据”吗?如果是这样,当函数本身应该创建一个新节点时我该怎么做?

谢谢!

/* Binary search trees in linkedlists!

*/

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

  typedef struct tnode Tnode; //Tree node 

  struct tnode {
    int data;
    Tnode *left;  //for smaller vals
    Tnode *right; //for larger vals
  };

Tnode * makeTnode( int data );

int main(int argc, char*argv[]){
  int data = 9;

  struct tnode new_node;

  Tnode * makeTnode( int data );

  printf("new_node's data is %d\n", new_node.data);

return 0;

}

Tnode * makeTnode( int data ){

  Tnode *new_node =(Tnode *)malloc(sizeof(Tnode));

  if(new_node == NULL) {
    fprintf(stderr, "Error: memory allocation failed\n");
    exit(1);
  }

  new_node->data = data;
  new_node->left = NULL;
  new_node->right = NULL;

  return(new_node);
}

最佳答案

您在 main() 中对 makeTnode 的函数调用不正确,任何返回结果都将不会被使用。看起来您想要做的是将结果保存为 new_node。因此,将您的主要功能更改为:

int main(int argc, char*argv[]){
  int data = 9;
  Tnode * new_node = makeTnode( data );
  printf("new_node's data is %d\n", new_node.data);
  free(new_node); //You should also clean up after yourself.
  return 0;
}

关于c - 尝试用 C 创建基本的二叉搜索树,但对 'unused variable' 感到困惑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33663099/

相关文章:

c++ - 如何使用控制台窗口中的输入顶点在 glut 中绘制多边形?

Java - 二叉搜索树有一个 iterator() 方法,难以实现

typescript - 如何检测 Typescript 中未使用的变量?

ios - 为什么在viewDidLoad中声明的数组在其他方法中使用时得到未使用的变量警告?

c - Mac终端编译运行C程序出错

c - 遍历字符串时,分配临时字符是否比取消引用更好?

c - 使用 VS2010 构建的可执行文件在 System32 中找不到 DLL

recursion - 用Java实现的二叉搜索树 - 递归查找元素

java - 递归 BST 插入不设置根 Java

typescript - 视觉代码 : How to detect dead typescript code