13,003,589 members (59,684 online)
Rate this:
See more:
Hi,
Is there any way to pass 2D dynamic array (pointer) to a function that has 3D dynamic array as parameter?

```void func(double*** s)
{
//do something with 3D dynamic array
}

void main()
{
double** s=new double*[10];
for(int i=0;i<10;i++)
s[i]=new double[10];

}```

update:

the code above is just a sample, not the hole code I have, so
the problem is in c# when we have unbounded array:
int[,] t;
we can get length of the array to work with like:
for(int i=0;i<array.getlenght();i++)>
I know a way to get the length of such arrays in c++, but first I need to copy my 2D array in a 3D array.
Posted 30-Oct-12 3:35am
mehdi_k952
Updated 31-Oct-12 22:36pm
v2
CPallini 30-Oct-12 10:21am

void func(double*** s)
This prototype is going to give you troubles (how can you iterate without knowing the dimensions?).
Sergey Alexandrovich Kryukov 30-Oct-12 10:31am

I have an impression that by some reason OP messes up arrays, jagged arrays and those chained pointers
double*** is not double arr[x][y][z] (but can be interpreted as jagged array).
The catch is: arr[x][y][z] is the same as arr[xxx] for the same object, for appropriate index.
--SA
Sergey Alexandrovich Kryukov 30-Oct-12 10:49am

--SA

Rate this:

## Solution 1

CPallini is absolutely right; see also my comment — in the discussion in comments to the question.

So far, your signature or parameter type make no much sense. You just need to learn arrays first, and then decide what you want to do with them and why. Please see:
http://www.cplusplus.com/doc/tutorial/arrays/[^].

Pay attention for the section "Multidimensional arrays" and this part: "Multidimensional arrays are just an abstraction for programmers, since we can obtain the same results with a simple array just by putting a factor between its indices".

You also need to understand how jagged arrays work:
http://en.wikipedia.org/wiki/Jagged_array[^],
http://www.functionx.com/cppcli/Lesson26.htm[^],
http://cboard.cprogramming.com/c-programming/139938-2d-array-pointer-crash-exe.html[^],
http://www.experts-exchange.com/Programming/Misc/Q_20790138.html[^].

(To my surprise, it wasn't easy to find a single simple article on jagged arrays. This topic is pretty simple for those who knows how to work with pointers, but… it looks like most discusses topics these days are all in .NET; even C++/CLI I found to be discussed in connection with this topic than C++.)

—SA
v2
mehdi_k 1-Nov-12 4:25am

Maybe I misunderstood you or maybe you did,
I've searched before to find an array that has no bound in c++ (Like int[,] t in c#) and I came up with dynamic arrays as above.
I have updated my question.

This is unrelated. There are no protection of the bounds typical for more save collection data structures implemented as classes, but if you violate the bounds, a big trouble can happen. General Protection Fault exception is the best case, but you can also damage neighboring memory silently, because hardware (CPU-level) protection will not be applied to every memory range, do you understand it?
--SA
mehdi_k 1-Nov-12 13:47pm

Well sorry to say this, but I don't understand it at all!!

And what can I do with that?

OK, (sigh...), behavior of the runtime and system on violation of array bounds is advanced stuff which not many developers understand.

But your "no bound in C++" simply means that you as a developer should be extremely responsible about not violating actual bounds of C or C++ arrays. Is that more clear? If you violate them, it can be way more trouble than with more high-level containers of data. In other words, C or C++ allows you to do the mistakes, but you should be very, very careful to avoid them, because they can cause big trouble. Is that also clear enough?

Now, more high-level concept of array is available in .NET, yes, but also in C++ STD class libraries. In .NET and C++ STD libraries, the bounds are checked up before actual addressing of the correspondent memory location. Eventually, you need to lean how to use such classes (and, maybe later, how to create them). It is way safer, but incur some performance cost. Is that also clear enough?

I hope now you can understand. If you see that it's reasonable, please accept the answer formally (green button) -- thanks.

--SA
mehdi_k 2-Nov-12 12:33pm

Yes, Now that's something..
Thank you for your time. (and I know how to accept the answer :) )

Sure.
Thank you, and good luck, call again.
--SA
Rate this:

## Solution 2

// Is there any way to pass 2D dynamic array (pointer) to a function that has 3D dynamic array as parameter?
Just apply an ampersand at the pointer of the "previous dimesions" :) :
```void func(double*** s,
int iSlotCount,
int iRowCount,
int iColCount)
{
//do something with 3D dynamic array
}

void main()
{
double** s = new double*[10];
for (int i = 0; i < 10; i++) {
s[i] = new double[10];
}

func(&s, 1, 10, 10);

for (i = 0; i < 10; i++) {
delete[] s[i];
}
delete[] s;
}```
mehdi_k 1-Nov-12 4:01am

look, the code above is not the real code I'm developing, it's just a sample.
the problem is I don't know the length of the 2D array.
Eugen Podsypalnikov 1-Nov-12 7:40am

A refactoring of the code that you have could change the situation.
For example, you could pass a reference to an object of a matrix-class,

Top Experts
Last 24hrsThis month
 OriginalGriff 243 RickZeeland 184 F-ES Sitecore 165 ppolymorphe 130 David_Wimbley 90
 OriginalGriff 4,940 ppolymorphe 2,527 RickZeeland 2,405 Kornfeld Eliyahu Peter 2,066 Richard Deeming 1,673