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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:18:55+00:00 2026-06-01T06:18:55+00:00

I’m working on a project of which I should store constant multi dimensional arrays.I

  • 0

I’m working on a project of which I should store constant multi dimensional arrays.I would like to do it with a pointer which points to multi-dimensional arrays.However, I could’nt succeeded.I write this code but it didn’t compiled.

int darray[1][2];
    int darray2[2][3];
    int (*p)[1][2];
    p= new int[2];
    p[0] = darray;
    p[1] = darray2;
  • 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-01T06:18:57+00:00Added an answer on June 1, 2026 at 6:18 am

    Well, let’s go through your code line by line (OK, I’ll lump the first two lines together)

    int darray[1][2];
    int darray2[2][3];
    

    OK, this defines two two-dimensional arrays in exactly the way you (probably) expect. Note that formally, those are actually arrays of arrays of ints. More exactly, darray is of type “array of 1 array of 2 ints each”, and darray2 is of type “array of 2 arrays of 3 ints each”

    int (*p)[1][2];
    

    This defines a pointer to a two-dimensional array of int, or to the first element of an array of such arrays. That is, the type of p is “pointer to array of 1 array of 2 ints each”.

    p= new int[2];
    

    Here you are allocating space for two ints (i.e. an one-dimensional array of ints, of length two), and new returns a pointer to the first element of that array, i.e. a pointer to int. You try to assign that pointer to int to p, which is a pointer to array of 1 array of 2 ints each, as mentioned above. This gives a type mismatch and should not compile.

    It’s not entirely clear what you want at that point, but given that you do assignments to p[0] and p[1] afterward, and given that the return value is assigned to p which is of type int (*)[1][2] the obvious choice would be p = new int[2][1][2];

    p[0] = darray;
    

    This again doesn’t work, because arrays in C++ are not first-class objects. That is, instead of assigning the value of darray to p[0] as the line would suggest (and p[0] indeed would have the right type for that) the rules of C++ say that the array darray is promoted to a pointer to its first element. That is, what this code actually tries to do is to assign a pointer to the first element of darray (of type int (*)[2]) to p[0] (of type int[1][2]), which of course fails. In C++, arrays are simply not assignable.

    p[1] = darray2;
    

    This suffers from the same problem, however note that even if arrays were first-class, assignable objects in C++, this still would be a type mismatch because p[1] is of type int[1][2] while darray2 is of type int[2][3].

    Note that you get around most of those limitations (apart from the last one) by just wrapping you array into a class C++11 actually provides a standard class template called std::array for this purpose):

    struct array12 { int data[1][2]; };
    array12 darray;
    array12* p;
    p = new array12[2];
    p[0] = darray;
    

    With a bit more of programming you could also handle the assignment from larger to smaller arrays (your darray2 case) by copying only part of the data by hand.

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

Sidebar

Related Questions

I would like to run a str_replace or preg_replace which looks for certain words
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:

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.