简体   繁体   中英

why two pointers used in structure in c

I read those about tree in c:

struct node
{
  int key_value;
  struct node *left;
  struct node *right;
};

/* insert a value to tree */
insert(int key, struct node **leaf)
{
    if( *leaf == 0 )
    {
        *leaf = (struct node*) malloc( sizeof( struct node ) );
        (*leaf)->key_value = key;
        /* initialize the children to null */
        (*leaf)->left = 0;    
        (*leaf)->right = 0;  
    }
    else if(key < (*leaf)->key_value)
    {
        insert( key, &(*leaf)->left );
    }
    else if(key > (*leaf)->key_value)
    {
        insert( key, &(*leaf)->right );
    }
}

I can't understand here: insert(int key, struct node **leaf) why two pointers **leaf, does *leaf ok? I am confused when to use two pointers.pls help, thank you very much!

In insert(int key, struct node **leaf) you are Passing the Address pointed by *leaf by C version of "Pass By Reference". And in insert(int key, struct node *leaf) you are passing the Address pointed by *leaf by Pass By Value method.Note C Parameter are always Passed by Value.

So, In This particular Case it doesn't matter if you use insert(int key, struct node **leaf) or insert(int key, struct node *leaf) both will achieve the same outputs.The only difference in this case is that in insert(int key, struct node **leaf) your passing the address by C version of Pass by Reference and in insert(int key, struct node *leaf) your passing the address by Pass By Value method.

Example Code A,

#include<stdio.h>

struct node
{
   int data;
};

void AddFive(struct node **t);

int main()
{
   struct node *n = NULL;
   n = new node;
   n->data = 5;
   printf("%d\n", n->data);
   AddFive(&n);
   printf("%d\n", n->data);

   return 0;
}

void AddFive(struct node **t)
{
    (*t)->data = (*t)->data+5;
}

Example Code B,

#include<stdio.h>

struct node
{
   int data;
};

void AddFive(struct node *t);

int main()
{
   struct node *n = NULL;
   n = new node;
   n->data = 5;
   printf("%d\n", n->data);
   AddFive(n);
   printf("%d\n", n->data);

   return 0;
}

void AddFive(struct node *t)
{
    t->data = t->data+5;
}

If you notice both Code A and Code B achieve the same output.

C has only pass by value. If pointer to node struct is used as parameter then any modification to passed pointer will not be seen in the caller function. In that case you have to return a pointer from the function insert . Pointer to pointer is used here to update the pointer passed to function insert .

When you want to change the value of a variable defined in main() through some function. Think what do you do. You send the address of that variable and then using that address, change the content of that variable.

Now, in an example cases, the variable is of int type, so sending the address of that variable would mean in the function, you have to receive it in a variable of type int *

void test(int* var) {
    *var++;
}

int main() {
    int integer = 1;
    test(&integer);
    printf("%d", integer);
    return 0;
}

To change the value of the variable integer you send the address of that to the function test() .


Now take this same situation, and think if you need to change the content of a variable which is itself a struct node * . Then you send the address of that variable, and receive it with a (struct node *)* . Thats what is happening in the example you posted.

You want the changes that is to be made to the leaf variable in the insert() fucntion to reflect in the calling function. For this to take place you send the address and change the content accordingly.

It is call by reference . if we have to change the value of *leaf then we should have its address thats why we used two *'s when is for pointer of leaf and other to get address of *leaf.

Case 1:

When you are passing a address of a variable then a single pointer is enough to access the variable

Example:

struct node a;

func(&a); // calling

In the func() definition:

func(struct node *a);

Here a points to the address of node. And we can access a using its address directly.

Case 2:

When you are sending the address of pointer variable:

struct node *a;

func(&a); // calling

Then in the function definition it should use a double pointer:

func(struct node **a);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM