tokiyashi
tokiyashi

Reputation: 85

C - Call by Reference

i wrote a simple program to sort numbers one by one for their repeat time and then inserts them one by one to tree. My problem is, i couldn't insert root's children because i couldn't change the function below to call-by-reference type.

q parameter below needs to hold the root's address value i think.

void insertNode(int data, node *q, node *parent){
    if(q == NULL){
        node *p = createNode(data);
        p -> parent = parent;
        p -> key = generateKey(p);
        int i;
        for(i = 0;table[i][1] != 0;i++);
        table[i][1] = p -> data;
        table[i][0] = p -> key;
        q = p;
    }
    else if(q -> left > q -> right || q -> left == q -> right){
        q -> right++;
        insertNode(data, q -> rightChild, q);
    }
    else if(q -> right > q -> left){
        q -> left++;
        insertNode(data, q -> leftChild, q);
    }
}

Upvotes: 1

Views: 933

Answers (3)

wildplasser
wildplasser

Reputation: 44250

The node allocated in the "if(q == NULL){ }" block is lost; once the function returns, there is no pointer to it available to the caller. (the q=p; assignment does nothing)

UPDATE: the **p enables you to remove the recursion as well:

void insertNode(int data, node **qRef, node *parent){
  node *q;
  int i;

  for (   ; (q = *qRef) ; parent=q ) {
    if(q->left >= q->right) {
      q->right++;
      qRef = &q->rightChild;
    }
    else {
      q->left++;
      qRef = &q->leftChild;
    }
  }


  *qRef = q = createNode(data);
  q->parent = parent;
  q->key = generateKey(q);

  for(i=0; table[i][1] != 0; i++) {;}
  table[i][1] = q->data;
  table[i][0] = q->key;

}

Upvotes: 2

Ed Swangren
Ed Swangren

Reputation: 124790

There is no such thing as "pass by reference" in C. If you need to assign a new value to the pointer passed into the function (not merely changing what the pointer points to) you will need to pass a pointer to the pointer, i.e.,

void insertNode(int data, node **q, node *parent){
    /* code */
    *q = p;
}

When you pass a pointer (or anything else) in C you are passing a copy of the pointer. So, this type of change is visible to callers of your function:

q->someVal = someOtherVal;

but this is not because you are only modifying the copy passed to the function:

q = p;

You need to add another level of indirection in order to modify the argument itself such that the change is visible outside of the function.

Upvotes: 7

Miroslav Bajtoš
Miroslav Bajtoš

Reputation: 10795

How about this?

void insertNode(int data, node **qRef, node *parent){
  node *q = *qRef; //<-- changed
  if(q == NULL){
    node *p = createNode(data);
    p -> parent = parent;
    p -> key = generateKey(p);
    int i;
    for(i = 0;table[i][1] != 0;i++);
    table[i][1] = p -> data;
    table[i][0] = p -> key;
    *qRef = p; //<-- changed
  }
  else if(q -> left > q -> right || q -> left == q -> right){
      q -> right++;
      insertNode(data, &(q -> rightChild), q); //<-- changed
  }
  else if(q -> right > q -> left){
      q -> left++;
      insertNode(data, &(q -> leftChild), q);  //<-- changed
  }
}

Upvotes: 0

Related Questions