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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:15:36+00:00 2026-05-26T16:15:36+00:00

I got the idea of pointers’ logic generally, but there is an unclear point

  • 0

I got the idea of pointers’ logic generally, but there is an unclear point for me that is related to the piece of code underlain.

#include <iostream>
using namespace std;

int main ()
{
int first = 50, 
second = 150;
int * p1, * p2;

p1 = &first;         //p1 is assigned to the address of first
p2 = &second;        //p2 is assigned to the address of second
*p1 = 100;           //first's value is changed as 100 by assigning p1's value to 100
*p2 = *p1;           //now p2's value should be 100 
p1 = p2;             //I think we want to change p1's adress as p2
*p1 = 200;           //I expect that the new value of p1 should be 200

cout << first << second;

return 0;
}

Program prints first=100 and second=200, but as I commented above, I expect that p1’s value is changed as 200. But It still remained as 100. What is the point of that?

  • 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-26T16:15:37+00:00Added an answer on May 26, 2026 at 4:15 pm

    You seem to be confusing a pointer value with the value a pointer points to.

    *p1 = 200;           //I expect that the new value of p1 should be 200
    

    p1 is a pointer, so its value is actually a memory location, the location of first. (something like 0xDEADBEAF). After this line:

    p1 = p2;
    

    p1 is left pointing to the memory location of second, so when doing

    *p1 = 200;
    

    its actually setting second to 200.

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

Sidebar

Related Questions

I've got to develop a multi-language code generator in C#. Well actually the idea
I know there are no pointers in C#, but I am trying to figure
I have got idea of how to build a website in 2 languages, i
Has anybody got an idea of how to create a .Contains(string) function using Linq
Has anyone got any idea why the menu is rendering below the rotating images
I'm doing a project with a lot of calculation and i got an idea
I just got this question on an interview and had no idea how to
this is the weirdest error I've ever got on Rails. Any idea what this
I got confused with __vfptr which I see through IDispatch/IUnknown pointers. I'm creating in-proc
Does anyone have an idea how to make a countdown ticker that shows the

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.