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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:33:02+00:00 2026-05-31T07:33:02+00:00

I have been trying to use 2D array as a parameter to a constructor

  • 0

I have been trying to use 2D array as a parameter to a constructor in my C++ class.

Header:

Matrix::Matrix(double **initComponents, int rows, int columns)

If I just go ahead and do regular calls to new and initialize sub arrays, pass a pointer to a constructor, its all good BUT I was thinking , can I just create static 2D array with initializer and pass a pointer to it, to a constructor?

double A[][3] = { {2.0, 3.0, 4.0} , {1.0, 3.0, 4.0} , {2.0, 3.0, 4.0} }; 
Matrix *mtx = new Matrix(A, 3, 3);

that does not work 🙁

MS Visual Studio compiler :

" error C2664: 'Matrix::Matrix(double **,int,int)' : cannot convert parameter 1 from 'double [3][3]' to 'double ** "

Can anyone explain why? I thought it will do an automatic conversion between reference and a pointer.

Second question is, why can I just declare A as double A[][], why do I have to specify the second dimension, even though I specify all values.

  • 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-31T07:33:04+00:00Added an answer on May 31, 2026 at 7:33 am

    An array does convert to a pointer, but it only works for one level. An array of arrays does not convert to a pointer to pointers.

    An array can be converted to a pointer because a pointer still provides a reasonable way to look up the values of the array, but an array of arrays is a fundamentally different thing than a pointer to pointers. An array of arrays is stored in memory contiguously. For example, if you had a definition like this:

    int a[2][2] = {{1,2},{3,4}};
    

    Then a[0][0] is stored in the first memory location, a[0][1] is stored in the second, a[1][0] in the third, and a[1][1] in the fourth. When you want to look up a particular element like a[i][j], the compiler can compute the location since it knows the size of the arrays:

    int value = ((int *)a)[i*2+j];
    

    Compare this to a pointer to pointers

    int a0[2] = {1,2};
    int a1[2] = {3,4};
    int **a = {a0,a1};
    

    Now when you use a[i][j], the compiler can’t directly know where to look. It first has to look at a[0] and see where that points to, and then look a second time to find the value.

    int *ap = a[i];
    int value = ap[j];
    

    Because the compiler has to use a different way of looking up the values in arrays of arrays and pointers to pointers, you can’t use them interchangeably.

    Likewise, in a declaration like this:

    int a[][2] = {{1,2},{3,4}};
    

    it is okay to leave off the size of the first dimension because the compiler will fill it in for you, but this only works for one level. In principle, the compiler could make a good guess about what the other dimensions should be, but it would really be a separate rule in the language.

    If you really want to be able to pass a 2D array, you can do it with a templated constructor like this:

    template<int rows, int cols>
    Matrix(const double (&initComponents)[rows][cols])
    {
      // initialize matrix elements here
    }
    

    And then you don’t even need to pass the size explicitly:

    double A[][3] = { {2.0, 3.0, 4.0} , {1.0, 3.0, 4.0} , {2.0, 3.0, 4.0} };
    Matrix *mtx = new Matrix(A);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to use routes.rb for creating a URL /similar-to-:product (where product
I have been trying to use the hibernate dialect for SQLite from http://code.google.com/p/hibernate-sqlite/ in
I have been trying to use/save the boolean value of a checkbox in other
I have been trying to use 3rd party tool called visiblox for rendering charts
I have been trying to use the document.getElementByID to pull information from an HTML
Right now I have been trying to use Launchpad's API to write a small
Ok I'm stumped. I have been trying to use Simpletip to create a tooltip
I have been trying to figure out why and how to use performSelector. I
I've been trying to use 'import poplib' to access gmail, since I have Pop
I have been trying to create a 2D array of vector for a game.

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.