Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8048231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:22:04+00:00 2026-06-05T06:22:04+00:00

When we want to change the value of an ordinary variable by in a

  • 0

When we want to change the value of an ordinary variable by in a function we pass it using call by reference. But I am not able to understand the intricacies when we have to pass a pointer variable(like a node of a binary tree) using call by refence. I understand that if we want to modify the poiter variable to point to another node we have to use call by reference. But what if we have to modify the data element of the root. I thought that to change it also we would need a call by reference. But the following code snippet is giving an output of 10, 10, 10 even though I have passed the root node of the tree using call by value in the function modifyTree. Am I missing something over here?

#include<stdio.h>
#include<stdlib.h>


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

/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
struct node* newNode(int data)
{
struct node* node = (struct node*)malloc(sizeof(struct node));

node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}

/*  This function sets the data fields of some of the nodes of tree to 10*/
void modifyTree(struct node* node)
{

node->data = 10;
node->left->data = 10;
node->right->data = 10;
}

int main()
{
struct node *root = newNode(1);
root->left            = newNode(2);
root->right          = newNode(3);
root->left->left     = newNode(4);
root->left->right   = newNode(5);

modifyTree(root);

printf("%d\n", root->data);
printf("%d\n", root->left->data);
printf("%d\n", root->right->data);

getchar();
return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:22:06+00:00Added an answer on June 5, 2026 at 6:22 am

    You are passing the pointer by value, but the pointer still points at the same thing. I’ll use some hypothetical values to demonstrate.

    In main you allocate a new struct node. Let’s say it gets created at memory location 0x12345. So now your struct node *root contains 0x12345.

    You now call modifyTree(root);. root gets passed by value to the root argument of modifyTree.

    That root now contains 0x12345. It’s pointing at the same memory location.

    So when you access that location with node->data = 10, you are accessing the same memory you created in main.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have firstName text that I want to change his value but it not
I want to change the variable value which is a member of a structure
Suppose I want to change the value of a variable when a flag is
I want to change the value of the selected tab but I don't know
I want change my class=value on select option using jQuery Example <select name=color_scheme id=color_scheme>
i have one component OutputText , i want to change its value using jquery,
I want to change the value of a particular string index, but unfortunately string[4]
I have a slider and want to change its value in some function. I
Hi I am developing Android application using Titanium.I want to change value of particular
I just want to get/change value of CheckBox with JavaScript. Not that I cannot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.