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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:45:52+00:00 2026-06-08T14:45:52+00:00

I wanted to get familiar with 2D variable sized arrays in c++, so I

  • 0

I wanted to get familiar with 2D variable sized arrays in c++, so I wrote a little program, but it doesn’t work. Here is the code:

#include <iostream>

using namespace std;

int main(){
int a,i;
cin>>a; //the width of the array is variable
int **p2darray;
p2darray = new int*[2]; //the height is 2
for (i = 0; i < 2; i++){
    p2darray[i] = new int[a];
}
i=0;
while(i!=a){
    p2darray[0][i]=i; //filling some numbers in the array
    p2darray[1][i]=2*i;
    i++;
}
i=0;
while(i!=a){
    cout<<p2darray[0][i]<<endl;
    cout<<p2darray[1][i]<<endl;
    i++;
}
return 0;
}

So why doesn’t it work?

  • 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-08T14:45:53+00:00Added an answer on June 8, 2026 at 2:45 pm

    The main problem is that when you say p2darray[i][0], your indices are backwards because you set the second dimension to the size the user enters, but you’re incrementing the first dimension to that number instead. This would normally cause a segfault. It should be p2darray[0][i] in all four cases. You also didn’t set i back to 0 before entering the printing loop, so it’s skipping the entire printing process.

    For a running program that illustrates the said corrections, see here.

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

Sidebar

Related Questions

I wrote a program in C# where I wanted to get a value from
I wanted to get the keyCode on keydown event in ckeditor. My code looks
When I wanted to get Android source code, I knew that I have to
I wanted to get a code review of my methods for creating a simple
I wanted to get Java running at 1.6xxx - I changed the JAVA_HOME variable
I wanted to get started using jMapping , but I cannot get a map
I wanted to get all of the textfields for my view, but I can't
I wanted to get the file extension of online file but problem is that
Where I work we're still on .Net 2.0, but I'm somewhat familiar with the
I wanted to add a function to my Slim application but I'm not familiar

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.