1

I was wondering if it is possible to pass part of a multidimensional array in c++. For example say I have an array of three dimensions (size X * Y * Z):

int array[X][Y][Z];

And say I also have a function that takes a two dimensional arrays (size Y * Z) as one of it's parameters:

void function(int stuff[Y][Z]);

Is it possible to call the function like this?

function(array[x_var]);

As in like pass the x_var slice of the three dimensional array only and therefore passing a two dimensional array[y][z] in effect..

Or would I have to use a new two dimensional array, copy the slice into that, then pass that into the function?

3
  • Consider using a decent linear algebra library: look at BLAS in www.boost.org. Then you can just pass an object. Commented Dec 10, 2013 at 9:17
  • 1
    Why not simply try it and see what happens? Commented Dec 10, 2013 at 9:19
  • Arrays decay to pointers, so in both cases you are passing a pointer, not sure what you are trying to save/do here. Commented Dec 10, 2013 at 9:21

2 Answers 2

1

You can! Only changing the dimensions order requires a rearranging, e.g. if you have 2-dimensional arrays, and a function which should take either a row or a column.

Sign up to request clarification or add additional context in comments.

Comments

1

I would suggest taking a look at the Boost Multidimensional Array Library as this makes it much easier to deal with slicing (which are called 'Views' in that library). If you make your own multidimensional arrays with the C style syntax int array[X][Y][Z] then you can only pass a slice in one dimension. For example, if you wanted to pass a row rather than a column then you can't easily do this with the C style syntax.

If you use a library like boost::multi_array then you will probably find that most of your code will work without modification (since you can also address elements as array[x][y][z]), the main change will be to defining the array to be the right size when you construct it. Incidentally, you can also resize it more easily than in the C-style case (where the dimensions have to be fixed at compile time)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.