473,320 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

dynamic multidimmensional arrays

RR
Hello I am trying to declare in my object something like

double **ma;

then do something like

ma = new int[x][y];

so I just want to declare a pointer to a double array and then
initialize the double array but I keep getting compiler error if
I do the above.

Is there an easy to do this
Jul 19 '05 #1
4 10177
RR wrote:
Hello I am trying to declare in my object something like

double **ma;

then do something like

ma = new int[x][y];

so I just want to declare a pointer to a double array and then
initialize the double array but I keep getting compiler error if
I do the above.


This is actually addressed in the FAQ:

[16.15] How do I allocate multidimensional arrays using new?
http://www.parashift.com/c++-faq-lit...html#faq-16.15

Check out the questions following 16.15 too.

--
Russell Hanneken
rg********@pobox.com
Remove the 'g' from my address to send me mail.
Jul 19 '05 #2
ma = new double*[100]; // 100 elements of type double*
for(i=0; i<100; i++)
ma[i]=new double[200]; // each ma[i] is an array

This is all clear if you think of

double ** ma;

as (double *) * ma. This means ma is a 1D array of type double*,
so each ma[i] will be a double*.

You allocate ma as you allocate any 1D array, with the appropriate
type (in this case (double*)). Then you allocate each ma[i].

Note that this way, ma is not contiguous (each row ma[i] is though).
If you want to make it contiguous, you still do

ma = new double*[100];

then you allocate the first row to have the size of all rows combined:

ma[0]=new double [100*200]; // all of ma is contiguous.

then you make each pointer ma[i] point where the i-th row should start
in ma[0]:

for (i=1; i<100; i++)
ma[i]=ma[i-1]+200;

On Sat, 25 Oct 2003 17:43:58 -0400, RR wrote:
Hello I am trying to declare in my object something like

double **ma;

then do something like

ma = new int[x][y];

so I just want to declare a pointer to a double array and then
initialize the double array but I keep getting compiler error if
I do the above.

Is there an easy to do this


Jul 19 '05 #3
RR wrote:
Hello I am trying to declare in my object something like

double **ma;

then do something like

ma = new int[x][y];

so I just want to declare a pointer to a double array and then
initialize the double array but I keep getting compiler error if
I do the above.

Is there an easy to do this


If you know one dimension is fixed in size then this works.

double (*ma)[229];

void f()
{
ma = new double[ 110 ][ 229 ];
}

Jul 19 '05 #4
"RR" <er********@yahoo.com> writes:
Hello I am trying to declare in my object something like

double **ma;

then do something like

ma = new int[x][y];

so I just want to declare a pointer to a double array and then
initialize the double array but I keep getting compiler error if
I do the above.

Is there an easy to do this


In the example above, you are neither declaring a pointer to a
double array, nor are you initializing anything with a double
array: you are trying to initialize a
pointer-to-pointer-to-double with an array-of-arrays-of-int. They
have pretty much nothing at all in common, and one certainly
isn't convertable to the other.

Part of your problem above may have just been to accidentally
type "int" where you meant "double"; however, it's still
important to realize that arrays and pointers are still *very*
different types. Don't let anyone try to tell you that arrays are
really pointers--that's what probably led you to this confusion
in the first place.

Even if the above had been:

double **ma = new double[x][y];

It'd still fail to compile. A (double [][]) can't be assigned to
a (double**): you wouldn't even be able to assign a (double [])
to a (double *), where it not the fact that an array expression
can be implicitly converted to a pointer to the first element of
an array. But that implicit conversion applied to a (double [][])
results in a (double (*)[]) [that is, a
pointer-to-array-of-doubles]... which is absolutely *not* the
same as a pointer-to-pointer-to-double.

So go get a good C++ book (Stroustrup, for example), and study
the difference between arrays and pointers until you are certain
you understand the difference.

If y is a constant, you could do:

double (*ma)[y];

followed by (or initialized similarly to):

ma = new double[x][y];

But I have a nagging suspicion that whatever you are doing may
not be the best way to address the problem you are building this
particular solution for. So: what is the context? What are you
trying to accomplish?

--
Micah J. Cowan
mi***@cowan.name
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: meyousikmann | last post by:
The following code just sets up and fills a dynamic array of integers. #include <cstdlib> int main() { int* intArray = NULL; int count; count = 20;
4
by: Scott Lyons | last post by:
Hey all, Can someone help me figure out how to pass a dynamic array into a function? Its been giving me some trouble, and my textbook of course doesnt cover the issue. Its probably something...
3
by: genc ymeri | last post by:
Hi, What can I use in C# for dynamic arrays ???? I have some records (struts in ..Net) and want to store them in a dynamic "arrays" or object list. I noticed the in C# arrays' length can't be...
60
by: Peter Olcott | last post by:
I need to know how to get the solution mentioned below to work. The solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below: >...
4
by: learnfpga | last post by:
Here is a little code I wrote to add the numbers input by the user.....I was wondering if its possible to have the same functionality without using dynamic arrays.....just curious..... ...
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
1
by: KioKrofov | last post by:
Hello, I am trying to find out how Dynamic Arrays are actually stored in memory. Really, I want to know how Vectors are stored in memory, but I deduce they are stored the same way. If you...
4
by: Sunny | last post by:
Hi, Is there a way in javascript to create Dynamic arrays or arrays on fly. Something Like: var "ptsgN"+sd = new Array(); Here sd is incrementing by 1. I have lots of data that I am...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.