>but it's still reseting root to be the new word each time
Because you're not calling the function properly. For the function to make changes to the root in main you must either save the return value (as in the code I gave you) or accept a pointer to a pointer as an argument. Here is some test code for you to play around with. Notice how the insert_node function is called:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct n {
char string[100];
struct n *left, *right;
} wordnode_t;
wordnode_t *insert_node(wordnode_t *root, wordnode_t *nodeptr)
{
if(root==NULL)
{
printf("if i stop here, root not made\n");
root=nodeptr;
printf("making root in theory. Root is %s\n", root->string);
root->right=NULL;
root->left=NULL;
}
else
{
wordnode_t *current=root;
wordnode_t **save = &root;
while(current!=NULL)
{
if (strcmp(nodeptr->string, current->string) == 0)
{
printf("were in equal to\n");
}
else {
if(strcmp(nodeptr->string, current->string)>0)
{
printf("were in greater than\n");
save = & current->right;
current=current->right;
}
else if(strcmp(nodeptr->string, current->string)<0)
{
printf("were in less than\n");
save = & current->left;
current=current->left;
}
}
}
*save = nodeptr;
nodeptr->left = NULL;
nodeptr->right = NULL;
}
return root;
}
void print_node ( char *value, int level )
{
int i;
for ( i = 0; i < level; i++ )
printf ( "\t" );
printf ( "%s\n", value );
}
void print_tree_structure ( struct n *node, int level )
{
if ( node == NULL )
print_node ( "~", level );
else {
print_tree_structure ( node->right, level + 1 );
print_node ( node->string, level );
print_tree_structure ( node->left, level + 1 );
}
}
int main ( void )
{
wordnode_t *root = NULL;
wordnode_t *word;
char buff[BUFSIZ];
while ( fgets ( buff, sizeof buff, stdin ) != NULL ) {
word = malloc ( sizeof *word );
strcpy ( word->string, buff );
root = insert_node ( root, word );
print_tree_structure ( root, 0 );
printf ( "\n--------------------\n" );
}
}
What was the current parameter for btw? I saw no real use for it so I removed it in my example.