0

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

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

谢谢!

/* 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);
}
4

2 回答 2

0

您在 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;
}
于 2015-11-12T01:50:41.803 回答
0
Tnode * makeTnode( int data );

是一个函数声明(这里是无用的,因为你已经声明了函数)

如果您想实际调用它,请使用:

new_node = makeTnode(data);

并确保new_node实际上是 a Tnode*(不是 a Tnode)。

于 2015-11-12T01:54:21.413 回答