Solved

User Defined Array Size

Posted on 1997-11-15
3
904 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

919 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now