Home > other >  C , question about references and pointers and about their functioning in function
C , question about references and pointers and about their functioning in function

Time:10-27

everybody! Really, I just want to understand what is going on with this references and pointers. Can you explain me, why in one case all is workking fine, but in another I receive nothing I will start with working case. So, there is a program (this is not mine):

#include <iostream>
#include <conio.h>

using namespace std;

struct node{
    double data;
    node *left;
    node *right;
};

node *tree = NULL;

void push(int a, node **t)
{
    if ((*t) == NULL)                   
    {
        (*t) = new node;                
        (*t)->data = a;                 
        (*t)->left = (*t)->right = NULL;       
        return;                         
    }

    if (a > (*t)->data) push(a, &(*t)->right); 
    else push(a, &(*t)->left);         
}

void print (node *t, int u)
{
    if (t == NULL) return;                  
    else 
    {
        print(t->left,   u);                   
        for (int i=0; i<u;   i) cout << "|";
        cout << t->data << endl;            
        u--;
    }
    print(t->right,   u);                       
}
 
int main ()
{
    int n;                             
    int s;                              
    cout << "Enter the amount of elements  ";
    cin >> n;                           
 
    for (int i=0; i<n;   i)
    {
        cout << "Enter the value  ";
        cin >> s;                       
 
        push(s, &tree);                 
    }

    cout << "Your binary tree\n";
    print(tree, 0);
    cin.ignore().get();
}

This is binary search tree made with struct, pointers and references. And it is work exactly fine But if i modify program in the way below it doesn't work. And i don't understand why, because

int *tree;
int **treePointer;
cout << (tree = *treePointer) <<endl; // Shows 1 i.e. true

Modified code:


#include <iostream>
#include <conio.h>

using namespace std;

struct node{
    double data;
    node *left;
    node *right;
};

node *tree = NULL;

void push(int a, node *t)
{
    if ((t) == NULL)                   
    {
        (t) = new node;                
        (t)->data = a;                 
        (t)->left = (t)->right = NULL;       
        return;                         
    }

    if (a > (t)->data) push(a, (t)->right); 
    else push(a, (t)->left);         
}

void print (node *t, int u)
{
    if (t == NULL) return;                  
    else 
    {
        print(t->left,   u);                   
        for (int i=0; i<u;   i) cout << "|";
        cout << t->data << endl;            
        u--;
    }
    print(t->right,   u);                       
}
 
int main ()
{
    int n;                             
    int s;                              
    cout << "Enter the amount of elements  ";
    cin >> n;                           
 
    for (int i=0; i<n;   i)
    {
        cout << "Enter the value  ";
        cin >> s;                       
 
        push(s, tree);                 
    }

    cout << "Your binary tree\n";
    print(tree, 0);
    cin.ignore().get();
}

As you see, all changes happen in push function argument. Why it is not working?

I am expecting that original program and modified will work the same

CodePudding user response:

This happens because of the following line in the modified code

(t) = new node;  

You are assigning memory to your pointer which is being created in a function call, a memory that will be lost after the function call.

Note that if you make changes to a member of a struct in a function call, the change will be reflected throughout because in that case, you will handle the member of the struct by reference.

But pointing the pointer to a new memory location created inside a function call will not propagate the changes. You need to pass the pointer by reference as well (thus pointer to a pointer).

Consider the following example -

#include <iostream>

typedef struct exampleStruct
{
    int a;
} exampleStruct;

void changeStruct(exampleStruct* S, int changedValue)
{
    S = new exampleStruct;
    S->a = changedValue;
    printf("Value of a inside Function = %d\n", S->a);
}

void changeStruct(exampleStruct** S, int changedValue)
{
    (*S) = new exampleStruct;
    (*S)->a = changedValue;
}

int main()
{
    exampleStruct* S = new exampleStruct;
    S->a = 100;
    printf("Value of a before calling function  = %d\n", S->a);
    changeStruct(S, 900);
    printf("Value of a after calling the function = %d\n", S->a);
    changeStruct(&S, 1300);
    printf("Value of a after calling 2nd function = %d\n", S->a);
}

The output is as follows -

Value of a before calling function  = 100
Value of a inside Function = 900
Value of a after calling the function = 100
Value of a after calling 2nd function = 1300

In the above example, similar to the modified code, the first function fails to propagate the changes, whereas the using pointer to the pointer does the trick

CodePudding user response:

In the modified code, when you do

(t) = new node;

you are setting the value of the local variable t to point to the new node. This value does not get put in your tree it only exists in the push function.

In the original

(*t) = new node;

sets the variable pointed to by t to the address of the new node. This means that the tree is modified.

  • Related