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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:51:46+00:00 2026-05-21T06:51:46+00:00

Hi I came across this bit of C++ code and am trying to learn

  • 0

Hi I came across this bit of C++ code and am trying to learn how pointers operate.

   int main ()
{
  int firstvalue, secondvalue;
  int * mypointer;

  mypointer = &firstvalue;
  *mypointer = 10;
  mypointer = &secondvalue;
  *mypointer = 20;
  cout << "firstvalue is " << firstvalue << endl;
  cout << "secondvalue is " << secondvalue << endl;
  return 0;
}

My questions are below:

  1. what exactly happens in memory when int *mypointer; is declared?
  2. what does mypointer represent?
  3. what does *mypointer represent?
  4. when *mypointer = 10; what happens in memory?
  • 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-21T06:51:46+00:00Added an answer on May 21, 2026 at 6:51 am
    1. what exactly happens in memory when int *mypointer; is declared?

    Sufficient memory is allocated from the stack to store a memory address. This will be 32 or 64 bits, depending on your OS.

    1. what does mypointer represent?

    mypointer is a variable on the stack that contains a memory address.

    1. what does *mypointer represent?

    *mypointer is the actual memory location pointed to by mypointer.

    1. when *mypointer = 10; what happens in memory?

    The value 10 is stored in the memory location pointed to by mypointer. If mypointer contains the memory address 0x00004000, for example, then the value 10 is stored at that location in memory.

    Your example with comments:

    int main ()
    {
      int firstvalue, secondvalue;   // declares two integer variables on the stack
      int * mypointer;               // declares a pointer-to-int variable on the stack
    
      mypointer = &firstvalue;       // sets mypointer to the address of firstvalue
      *mypointer = 10;               // sets the location pointed to by mypointer to 10.
                                        In this case same as firstvalue = 10; because
                                        mypointer contains the address of firstvalue
      mypointer = &secondvalue;      // sets mypointer to the address of secondvalue
      *mypointer = 20;               // sets the location pointed to by mypointer to 10. 
                                        In this case same as secondvalue = 20; because 
                                        mypointer contains the address of secondvalue
      cout << "firstvalue is " << firstvalue << endl;
      cout << "secondvalue is " << secondvalue << endl;
      return 0;
    }
    

    Try this code and see if that helps:

    int main ()
    {
      int firstvalue, secondvalue;   
      int * mypointer;               
    
      cout << "firstvalue is " << firstvalue << endl;
      cout << "secondvalue is " << secondvalue << endl;
      cout << "mypointer is pointing to " << mypointer << endl;
    
      mypointer = &firstvalue;       
    
      cout << "firstvalue is " << firstvalue << endl;
      cout << "secondvalue is " << secondvalue << endl;
      cout << "mypointer is pointing to " << mypointer << endl;
    
      *mypointer = 10;               
    
      cout << "firstvalue is " << firstvalue << endl;
      cout << "secondvalue is " << secondvalue << endl;
      cout << "mypointer is pointing to " << mypointer << endl;
    
      mypointer = &secondvalue;      
    
      cout << "firstvalue is " << firstvalue << endl;
      cout << "secondvalue is " << secondvalue << endl;
      cout << "mypointer is pointing to " << mypointer << endl;
    
      *mypointer = 20;               
    
      cout << "firstvalue is " << firstvalue << endl;
      cout << "secondvalue is " << secondvalue << endl;
      cout << "mypointer is pointing to " << mypointer << endl;
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across this code: #include<stdio.h> void main() { int x; float t; scanf(%f,&t);
I thought I understood variable scope until I came across this bit of code:
I came across this bit of vba code posted in another SO question. Is
I recently came across this genius bit of code on HTTP POST from PHP,
I came across this bit of code: n = args[0] as Long [*n..1, n].any{
So I came across this bit of php code: <?php $long_url = http://example.com; $bit_ly
I came across this strange bit of CSS tonight... display: inline !ie; Now I've
I was doing a bit research and I came across this comment. I have
Came across this code: <?php require_once 'HTTP/Session/Container/DB.php'; $s = new HTTP_Session_Container_DB('mysql://user:password@localhost/db'); ini_get('session.auto_start') or session_start();
I came across this Linq to Sql code in an application I am maintaining:

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.