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 9232285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:16:41+00:00 2026-06-18T06:16:41+00:00

I initialized a test case as a global variable, here: void InsertNode(BSTNode* &t, const

  • 0

I initialized a test case as a global variable, here:

void InsertNode(BSTNode* &t, const int &key) {
    if (t == NULL) {
        t = new BSTNode;
        t->key = key;
        t->left = t->right = NULL;
    } else {
        if (key != t->key) {
        if (key < t->key)
                InsertNode(t->left, key);
            else
                InsertNode(t->right, key);
        }
    }
}

BSTNode t1[] = {
 {4, &t1[1], &t1[2]},
 {2, &t1[3], &t1[4]},
 {6, &t1[5], &t1[6]},
 {1, NULL, NULL},
 {3, NULL, NULL},
 {5, NULL, NULL},
 {7, NULL, NULL}
};

int main() {
    InsertNode(t1, 0);
    return 0;
}

However, when I try to modify t1, it gives me an error:

invalid initialization of non-const reference of type 'BSTNode*&' from a temporary of type 'BSTNode*'

Could someone explain this for me? Thank you!!

  • 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-18T06:16:42+00:00Added an answer on June 18, 2026 at 6:16 am

    The problem is that your function is stating that it may change the pointer:

    void InsertNode(BSTNode* &t, const int &key) {
    

    it is taking a reference to a non-const pointer as a parameter, so it has the potential of modifying that pointer. However when you do this call:

    InsertNode(t1, 0);
    

    you are passing in a non-modifiable pointer, since t1 is an array. An array can be used like a pointer, but you can’t make that pointer point somewhere else.

    One way to deal with this would be to have two different functions:

    void InsertNode(BSTNode* &t, const int &key);
    
    void AddNode(BSTNode* t, const int &key) {
        assert(t!=NULL);
        if (key != t->key) {
        if (key < t->key)
                InsertNode(t->left, key);
            else
                InsertNode(t->right, key);
        }
    }
    
    void InsertNode(BSTNode* &t, const int &key) {
        if (t == NULL) {
            t = new BSTNode;
            t->key = key;
            t->left = t->right = NULL;
        } else {
            AddNode(t,key);
        }
    }
    

    And then call

    AddNode(t1, 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please explain why pointers aren't initialized to NULL ? Example: void test(){
Why does the array a not get initialized by global variable size ? #include<stdio.h>
I initialized an Array as Double[][] myarr = Enumerable.Repeat(new double[12], 13).ToArray(); Then in a
I'm writing an integration test where an application context xml is initialized during startup.
Here you go: root@Dell: /tmp # mkdir test; cd test root@Dell: /tmp/test # git
I've become a fan of nested test case contexts in things like RSpec and
The following compiles successfully: struct Foo {} void Test() { Foo foo; foo.ToString(); }
I have a very general question and have prepared a simple test case. When
Here is the setup: end_date = DateTime.parse('2010-01-01 12:00:00-04') and sometimes it's initialized: end_date =
I initialized a Mercurial project on Machine A , committed my changes and uploaded

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.