Solved

User Defined Array Size

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

757 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

22 Experts available now in Live!

Get 1:1 Help Now