Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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.
So if there is a way to do this, please help.
Posted 30-Oct-12 4:35am
mehdi_k844
Edited 31-Oct-12 23:36pm
v2
Comments
CPallini at 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 at 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 at 30-Oct-12 10:49am
   
So, I provided some links in my answer.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

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://bytes.com/topic/c/answers/852849-array-pointers-objects[^],
http://bytes.com/topic/c/answers/778705-array-pointers[^],
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
  Permalink  
v2
Comments
mehdi_k at 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.
Sergey Alexandrovich Kryukov at 1-Nov-12 11:08am
   
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 at 1-Nov-12 13:47pm
   
Well sorry to say this, but I don't understand it at all!!
Sergey Alexandrovich Kryukov at 1-Nov-12 14:11pm
   
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 at 2-Nov-12 12:33pm
   
Yes, Now that's something..
Thank you for your time. (and I know how to accept the answer :) )
Sergey Alexandrovich Kryukov at 2-Nov-12 13:54pm
   
Sure.
Thank you, and good luck, call again.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

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" Smile | :) :
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;
}
  Permalink  
Comments
mehdi_k at 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 at 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,
that could answer any question about its dimensions :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 1 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100