Pointer function for pointer

-1

I have the following function

NOTE: In this function is only the first case, not to get too large. Since the error occurs at the first insertion.

int insertAVL_TreeR(struct AVL_TreeNode **node, void *elem, int *h) {
        if (!node) {//se o nó não existir, é criado um novo no
            AVL_TreeNode *novo = (AVL_TreeNode*) malloc(sizeof (AVL_TreeNode));
            novo->elem = elem;
            novo->fb = 0;
            novo->left = NULL;
            novo->right = NULL;
            node = novo;
            *h = 1; //haverá mudança na altura
        } else {

What is called inside right function

int insertAVL_Tree(struct AVL_Tree *tree, void *elem) {
    if (!tree)return 0;
    int *h = (int*) malloc(sizeof (int));
    *h = 1;
    AVL_TreeNode **p = &tree->root;
    int x = insertAVL_TreeR(*p, elem, h); //a chamada se dá nesta linha
    return x;
}

I want to know the correct way to pass this pointer to pointer. For inside the function I get a warning and everything works fine, but when it goes back to main, avl-> root (previously initialized as NULL) keeps pointing to NULL.

My structs are esssa

typedef struct AVL_TreeNode
{
  void *elem;
  struct AVL_TreeNode *left;
  struct AVL_TreeNode *right;
  int fb;
}AVL_TreeNode;

and

typedef struct AVL_Tree 
{
  struct AVL_TreeNode *root;
}AVL_Tree;
    
asked by anonymous 25.02.2016 / 02:25

1 answer

2

The call of the insertAVL_TreeR method is wrong, the node parameter is a double pointer, which would be its AVL_TreeNode **p variable. and when calling the method, you send as a parameter the memory address that is within p . The correct one would be to pass p without * , because this would pass the memory address that has the reference of its tree->root . Staying like this:

int x = insertAVL_TreeR(p, elem, h);

Now in your insertAVL_TreeR method the error is where you assign the value to node . node is an independent variable that stores a memory address, assign some value to it, it will not change the node of your tree in any way, so how to do it? It is known that the value of node is the address that stores your node, so you need to change the value inside the node in this way.

(*node) = novo;

/*
node = <Endereço de tree->root>
(*node) = tree->root
*/

This way you can change the value of your node.

    
25.02.2016 / 05:36