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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:38:43+00:00 2026-05-17T18:38:43+00:00

there is a topic about this subject which is working with arrays but I

  • 0

there is a topic about this subject which is working with arrays but I can’t make it work with matrices.

(Topic: C++ array size dependent on function parameter causes compile errors)

In summary:

  long rows, columns;
  cin >> rows >> columns;
  char *a = new char [rows];    

compiles great in visual studio, but:

  char **a = new char[rows][columns]; 

or

  char *a[] = new char[rows][columns];

or

  char *a[] = new char[rows][columns]();

or

  char **a = new char[rows][columns]();

don’t compile at all.

Any help? Thank you

  • 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-17T18:38:43+00:00Added an answer on May 17, 2026 at 6:38 pm

    The array-new operator only allocates 1-dimensional arrays. There are different solutions, depending on what sort of array structure you want. For dimensions only known at runtime, you can either create an array of arrays:

    char **a = new char*[rows];
    for (int i = 0; i < rows; ++i) {
        a[i] = new char[columns];
    }
    

    or an array of elements with an associated array of pointers to the first element of each row (requiring just two hits to the memory allocator):

    char *a = new char[rows*columns];
    char **a = new char*[rows];
    for (int i = 0; i < rows; ++i) {
        a[i] = a + i*columns;
    }
    

    Either one will let you access matrix elements via a[row][column].

    An alternate solution is to just use the one-dimensional array and generate indexes by hand:

    char *a = new char[rows*columns];
    ...
    a[columns*row + column]
    

    This is probably faster than the double-indirection required in the first two solutions.

    You could of course wrap this in a class to preserve a semblance of 2D indexing syntax:

    class MatrixWrapper {
        ...
        char& operator()(int row, int column) { return a_[columns*row + column]; }
        ...
    };
    ...
    a(row, column)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There have been many debates about this topic already here, but none of them
I know that there are a lot of questions about this topic, but unfortunately
So there are a few things I know about this topic but I am
I know there's a lot of questions about this topic but I have not
There's a lot of conflicting information about this topic. So let's try to agree
I realize there are similar questions on this topic, but I still cannot find
Reading online about this topic is confusing. Is there a way to cut around
I know there are plenty of questions here already about this topic (I've read
So I know there have been a couple of posts around about this topic,
This is my second question about this topic, the original question can be found

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.