Terry
Terry

Reputation: 95

C Pointer of Pointer of struct and reference

I am trying to implement a BST in C. Here is the code :

int main(int argc, char* argv[]) {
    int_bst_node_t * tree_p = NULL;
    test_insert(&tree_p, 40);
}

static void test_insert(int_bst_node_t ** t_p, int n) {
    printf("inserting %d into tree ", n);
    printf("\n");
    if (int_bst_insert(t_p, n)) {
        printf("result ");
        print_tree_elements(*t_p);
        printf("\n");
    }
    else {
       printf("insert failed\n");
    }
}

Code in other file :

// Create a node, assign values
bool createNode(int n, int_bst_node_t *node) {
    int_bst_node_t *localNode = (struct int_bst_node*)malloc(sizeof(int_bst_node_t));
    if( localNode == NULL )
    {
        puts("\n Unable to allocate memory");
        return false;
    }
    localNode->data = n;
    localNode->left = NULL;
    localNode->right = NULL;

    node = localNode;
    //////    Prints right values 
    printf("\n LocalNode Data: %d  Node Data: %d", localNode->data, node->data);
    free(localNode);
    return true;
}

/* 
 * insert n into *t_p
 * does nothing if n is already in the tree
 * returns true if insertion succeeded (including the case where n is already
 * in the tree), false otherwise (e.g., malloc error)
*/
bool int_bst_insert(int_bst_node_t ** t_p, int n) {
    if (*t_p == NULL) {
        printf("*t_p %d IS null", *t_p);
        int_bst_node_t node;
        // Pass node as a ref, so data is updated
        if (createNode(n, &node) == false)
            return false;

        // Prints invalid data
        printf("\n Data of new node : %d", node.data);
        *t_p = &node;

        /*
          Need  to assign node to *t_p, so it is updated, and prints the values
          when calling next function in test_insert()

        int_bst_node_t *t = *t_p;
        printf("\n Data of *tp node : %d", t->data);  
        */     
     } else 
        printf("*t_p %d is NOT null", *t_p);

    printf("\n"); 
    return true;
 }

I am not able to set the value/ref of node to t_p. This is for just the root node; further it would be more nodes. I am not able to get the concept of ** to update the value. I tried variations, all fails.

I will be glad, if someone can help me with this.

Thank you.

Upvotes: 1

Views: 37

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 311126

The function createNode does not make sense at least because it accepts a pointer to a node by value. And after allocating memory for a node you at once free it making the pointer that pointed to the allocated memory invalid.

int_bst_node_t *localNode = (struct int_bst_node*)malloc(sizeof(int_bst_node_t));
//...
node = localNode;
//...
free(localNode);

The function int_bst_insert also does not make sense. Apart from errors like this

*t_p = &node;

it should insert a node relative to the values of already existent nodes in the binary search tree.

Pay attention to that the functions shall not output any message.

The functions can be defined the following way.

int_bst_node_t * createNode( int n ) 
{
    int_bst_node_t *localNode = malloc(sizeof(int_bst_node_t));

    if( localNode != NULL )
    {
        localNode->data = n;
        localNode->left = NULL;
        localNode->right = NULL;
    }

    return localNode;
}

and

bool int_bst_insert( int_bst_node_t ** t_p, int n ) 
{
    while ( *t_p != NULL )
    {
        if ( n < ( *t_p )->data )
        {
            t_p = &( *t_p )->left;
        }
        else
        {
            t_p = &( *t_p )->right;
        }
    }

    *t_p = createNode( n );  

    return *t_p != NULL;
}

Upvotes: 1

Related Questions