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

  • Home
  • SEARCH
  • 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 6235697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:42:53+00:00 2026-05-24T10:42:53+00:00

I have a pointer which is pointing to an integer variable. Then I assign

  • 0

I have a pointer which is pointing to an integer variable. Then I assign this pointer to a reference variable. Now when I change my pointer to point some other integer variable, the value of the reference variable doesn’t change. Can anyone explain why?

int rats = 101;
int * pt = &rats;
int & rodents = *pt;                                // outputs    
cout << "rats = " << rats;                          // 101
cout << ", *pt = " << *pt;                          // 101
cout << ", rodents = " << rodents << endl;          // 101
cout << "rats address = " << &rats;                 // 0027f940
cout << ", rodents address = " << &rodents << endl; // 0027f940
int bunnies = 50;
pt = &bunnies;

cout << "bunnies = " << bunnies;                    // 50
cout << ", rats = " << rats;                        // 101  
cout << ", *pt = " << *pt;                          // 50
cout << ", rodents = " << rodents << endl;          // 101
cout << "bunnies address = " << &bunnies;           // 0027f91c
cout << ", rodents address = " << &rodents << endl; // 0027f940

We assigned pt to bunnies, but the value of rodents is still 101. Please explain why.

  • 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-05-24T10:42:55+00:00Added an answer on May 24, 2026 at 10:42 am

    The line

    int & rodents = *pt;
    

    is creating a reference to what pt is pointing to (i.e. rats). It’s not a reference to the pointer pt.

    Later, when you assign pt to point to bunnies, you would not expect the rodents reference to change.

    EDIT: To illustrate @Als point, consider the following code:

    int value1 = 10;
    int value2 = 20;
    int& reference = value1;
    cout << reference << endl; // Prints 10
    reference = value2; // Doesn't do what you might think
    cout << reference << endl; // Prints 20
    cout << value1 << endl; // Also prints 20
    

    The second reference assignment does not change the reference ltself. Instead, it applies the assignment operator (=) to the thing referred to, which is value1.

    reference will always refer to value1 and cannot be changed.

    It’s a little tricky to get your head around at first, so I recommend you take a look at Scott Meyer’s excellent books Effective C++ and More Effective C++. He explains all this much better than I can.

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

Sidebar

Related Questions

I am using C. I have a wchar_t pointer which pointing a a file
I have a pointer to an object, which I want to release, and then
how do I make a pointer to a multidimensional array, which have a unknown
I have some Visual C++ code that receives a pointer to a buffer with
Say I have a struct s with an int pointer member variable i. I
For trying out some pointer stuff, I made an instance variable in an existing
If I have a pointer that is pointing to an element in a vector,
I have a c pointer to a structre type called uchar4 which looks like
I have a pointer which keeps incrementing and I need to return the head
i have a input tag which is non editable, but some times i need

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.