Solved

User Defined Array Size

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

808 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