Solved

User Defined Array Size

Posted on 1997-11-15
3
951 Views
Last Modified: 2012-06-27
int * aPointer;
aPointer = new int[10][10];

Why won't it let me assign a multi-dimensional array?
0
Comment
Question by:BabyFace
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 2

Accepted Solution

by:
wpinto earned 50 total points
ID: 1173282
Hi BabyFace,

When new is used to allocate a single object, it yields a pointer to that object; the resultant type is new-type-name * or type-name *. When new is used to allocate a singly dimensioned array of objects, it yields a pointer to the first element of the array, and the resultant type is new-type-name * or type-name *. When new is used to allocate a multidimensional array of objects, it yields a pointer to the first element of the array, and the resultant type preserves the size of all but the leftmost array dimension. For example:

new int[10][10]
yields type int (*)[10].

Therefore, the following code will not work because it attempts to assign a pointer to an array of int with the dimensions [10] to a pointer to type int:

int *aPointer;
aPointer = new int[10][10];

The correct expression is:

int (*aPointer)[10];
aPointer = new int[10][10];

Hope this helps

Wilfred
0
 
LVL 84

Expert Comment

by:ozo
ID: 1173283
line 3: error(3611): a value of type "int (*)[10]" cannot be
          assigned to an entity of type "int *"
  aPointer = new int[10][10];
           ^
//try
int (*aPointer)[10];
0
 
LVL 1

Author Comment

by:BabyFace
ID: 1173284
Good job.

Thanks.

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question