0

Created a basic binary search tree, utilising linkedlists AND also trying to input 'data' into the function (because it requires it). However, I keep getting the 'unused variable' error even though I'm using it?

Is it because I'm not returning 'data'? If so, how am I supposed to when the function itself is supposed to be creating a new node?

Thanks!

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

2 Answers2

0

Your function call to makeTnode in main() is incorrect and any return result would be unused. It looks like what you want to do is save that result as new_node. So change your main function to this:

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;
}
Matt O
  • 1,336
  • 2
  • 10
  • 19
0
Tnode * makeTnode( int data );

is a function declaration (and a useless one here, because you've already declared the function)

If you want to actually call it, use:

new_node = makeTnode(data);

and make sure new_node is actually a Tnode* (not a Tnode).

user253751
  • 57,427
  • 7
  • 48
  • 90