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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:13:35+00:00 2026-06-01T23:13:35+00:00

As a beginner to c++ I’m struggling to understand pointers and array. I wrote

  • 0

As a beginner to c++ I’m struggling to understand pointers and array. I wrote the following program:

int main (void){
int p[3]={0};
int * iptr = new int [4];

iptr++;
*iptr=2;
iptr++;
*iptr=3;

for (int i=0;i<4;i++){
    cout << "iptr: " << *iptr << endl;
    iptr++;
}

return 0;
}

However I’m not getting the expected results. I would have expected elements [1] and [2] to have the values 2 and 3 respectively. However I get a result along the lines of:

iptr: 3
iptr: -842150451
iptr: -33686019
iptr: 0

Can someone please tell me where I’m going wrong in my understanding?

Thanks
Dan

  • 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-01T23:13:36+00:00Added an answer on June 1, 2026 at 11:13 pm

    The problem is here:

    int * iptr = new int [4];
    
    iptr++;
    *iptr=2;
    iptr++;
    *iptr=3;
    

    after you call iptr++, iptr will no longer point to the beginning of the array. After you set the values, you should reset iptr to the original position ( iptr -= 2 ) or use an auxiliary pointer.

    So 2 possible solutions:

    iptr++;
    *iptr=2;
    iptr++;
    *iptr=3;
    
    iptr -= 2;
    
    for (int i=0;i<4;i++){
        cout << "iptr: " << *iptr << endl;
        iptr++;
    }
    

    or (cleaner):

    int* aux = iptr;
    aux++;
    *aux=2;
    aux++;
    *aux=3;
    
    for (int i=0;i<4;i++){
        cout << "iptr: " << *iptr << endl;
        iptr++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im beginner in android development. I ve just created a button in main.xml file
Beginner in DBPedia/SPARQL/Semantic data. I ran following queries to find out the sport for
iOS beginner here. I'm using the following code to save my facebook accessToken and
iam beginner in Objective-C, i tried to compile small Hello world program to start,iam
Beginner's question: I dared to introduce constants into my little program. I first resisted,
Beginner programmer here. So bear with me. I have a simple python program. print
(Lisp beginner) I need to create a 2D array, and initialize each cell in
Beginner question. How do I substitute: $_SESSION['personID'] for {personID} in the following: public static
another beginner problem. Why isn't the following code with an asp.net page not working?
Complete beginner here. I want to create a new tab on each page that

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.