Striding across members of a vector of structures - c++

Is there an easy way to stride through an STL vector of structures by member? In other words, if I have a struct like this:
struct foo {
double x, y, z;
};
in a vector std::vector<foo> bar(20), can I stride across the array picking out x from each struct?
I've tried this, but it does not seem to work:
for (int i=0; i<20; ++i)
{
double xx = (&bar[0].x + i*sizeof(bar[0]))->x;
}
Why doesn't that work? Does sizeof(bar[0]) not account for the padding between structs?
Note: I realize this is a really silly way to access x in a loop, but this loop is just an experiment to see if the stride works or not.
If it helps, I want to do this so I can pass bar to a library routine that accepts a pointer and a stride as constructor parameters to its own internal datatype. I could, of course, convert my code from AoS to SoA, but I don't want to do that unless I absolutely have to.

I think I'd compute the stride directly instead, using something like:
struct point {
double x, y, z;
};
int main() {
point points[2];
std::cout << "stride = " << (char *)(&(points[1].x)) - (char *)(&(points[0].x)) << "\n";
}

&bar[0].x is a pointer to double. You're adding i*sizeof(bar[0]) to it.
The effect is that the address stored in the pointer increases by i*sizeof(bar[0])*sizeof(double) which is not what you expect.
A correct expression is
&bar[0].x + i*sizeof(bar[0])/sizeof(double)

&bar[0].x is a pointer to double. To add the appropriate shift, you would need to cast it into char*, then double* again.
double xx = *reinterpret_cast<double*>(reinterpret_cast<char*>(&bar[0].x) + i*sizeof(bar[0]));
In anycase, you should really consider using standard algorithms for your purpose.

Related

Passing pointer of multi-dimensional pointer array to a function

ok so suppose I have a function myFunction. Then in main i have a multi dimensional array of pointers. I want to pass a pointer to this array of pointers into myFunction. How would I do that? I know that If you want to pass an int to my function, one can write the function as
myfunct( int x) { ...}
What would that type of x be if I have to pass a pointer to an array of pointers? Thanks in advance :D
Typically you want to modify the elements of an array rather then the actual pointer. The actual pointer is given by malloc and if you change it, by writing directly to the value, it won't affect the memory allocation (except you might loose the initial pointer...).
This might be what you're looking for in a 2D array.
void myfunct(int** ptr,int items, int array_items)
{
//some code
}
int main(int argc, char *argv[])
{
const auto items = 5;
const auto array_items = 7;
int** multi_dimensional_array = reinterpret_cast<int**>(std::malloc(items * sizeof(int*)));
for (auto i = 0 ;i < items;++i)
{
multi_dimensional_array[i] = static_cast<int*>(std::malloc(sizeof(int) * array_items));
}
myfunct(multi_dimensional_array,items,array_items);
//deallocate
}
Wrap your multidimensional array inside a class. That way you can carry the data and dimensions in one block and passing it around is as simple as moving around any other class.
Remember to observe the Rules of Three, Five, and Zero, whichever best applies to how you store your array inside your class. std::vector is a personal favourite because it allows you to use the Rule of Zero.
For example:
#include <iostream>
#include <vector>
struct unspecified
{
};
template<class TYPE>
class TwoDee{
int rows;
int cols;
std::vector<TYPE> data;
public:
TwoDee(int row, int col):rows(row), cols(col), data(rows*cols)
{
// does nothing. All of the heavy lifting was in the initializer
}
// std::vector eliminates the need for destructor, assignment operators, and copy
//and move constructors. All hail the Rule of Zero!
//add a convenience method for easy access to the vector
TYPE & operator()(size_t row, size_t col)
{
return data[row*cols+col];
}
TYPE operator()(size_t row, size_t col) const
{
return data[row*cols+col];
}
};
void function(TwoDee<unspecified *> & matrix)
{
// does stuff to matrix
}
int main()
{
TwoDee<unspecified *> test(10,10);
function(test);
}
To directly answer your question, typically the type passed will be int * for a vector of int, and int ** for a 2D array of int
void myfunct( int **x)
{
x[2][1] = 25;
return;
}
If for some reason you wanted that to be an array of int pointers instead of int you need an extra *.
void myfunct( int ***x)
{
*(x[2][1]) = 25;
return;
}
Let me first try to interpret the exact type that you want to deal with. I suppose in your main function there is a "multidimensional array" which stores pointers for each element. As an example, let's say you have a 3-dimensional array of pointer to integer type.
Assume that you know the size of the array:
C style array will look like this:
int *a[4][3][2];
that means a is a 4x3x2 array, and each element in the array is a pointer to integer. So overall you now have 24 pointers to integer in total, as can be seen by testing the result of sizeof(a) / sizeof(int*) (the result should be 24). Okay, so far so good. But now I guess what you want is a pointer to the array a mentioned above, say b, so b is defined
int *(*b)[4][3][2] = &a;
Notice that although now b looks intimidating, in the end it is just a pointer which just stores an address, and sizeof(b) / sizeof(int*) gives 1 as the result. (The * inside parenthesis indicates b is pointer type, so b is a pointer to a "multidimensional array" of pointers to integer.)
Now to pass b to myFunction, just give the same type of b as argument type in the declaration:
void myFunction(int *(*x)[4][3][2]) {
// do something
}
And that's it! You can directly use myFunction(b) to invoke this function. Also, you can test that inside myFunction, x is still of the size of one pointer, and *x is of the size of 24 pointers.
*Note that since we are passing a pointer to array type into the function, the array-to-pointer decay does not apply here.
Assume you don't know the size of the array at compile time:
Say you have int N1 = 4, N2 = 3, N3 = 2; and you want to initialize a N1xN2xN3 array of pointer to integer, you cannot directly do that on the stack.
You could initialize use new or malloc as suggested in #Mikhail's answer, but that approach takes nested loops for multidimensional arrays and you need to do nested loops again when freeing the memory. So as #user4581301 suggests, std::vector provides a good wrapper for dynamic size array, which do not need us to free the memory by ourselves. Yeah!
The desired array a can be written this way (still looks kind of ugly, but without explicit loops and bother of freeing memory)
std::vector<std::vector<std::vector<int*>>> a (N1,
std::vector<std::vector<int*>> (N2,
std::vector<int*> (N3)
)
);
Now, b (the pointer to a) can be written as
auto *b = &a;
You can now pass b with
void myFunction(std::vector<std::vector<std::vector<int*>>>* x) {
// do something
}
Notice that the * before x means x is a pointer.

void pointer to vector elements

I want to have a void pointer to a vector.
void *para;
vector<double> x(2);
x[0] = 0;
x[1] = 1;
para = &x;
I can now use the vector like this.
vector<double> k = *(static_cast<vector<double>*>(para));
cout << k[0] << "\n";
Now I want to access the elements of the vectors through the pointer. How can I do that? But now I want to get the elements of the vector x, directly via the void pointer para, without using the new vector k. Something like this:
double k = ??? // here should be the element of x via para
Thank's in advance.
Now I want to access the elements of the vectors through the pointer.
This has two steps: reinterpret the pointer as a vector, then access elements.
How can I do that? But now I want to get the elements of the vector x, directly via the void pointer para, without using the new vector k.
void *para;
vector<double> x(2);
para = &x;
// step 1: reinterpret the pointer as a vector
auto *voidToVector = reinterpret_cast< vector<double>* >(para);
// step 2: access elements
double k = (*voidToVector)[0];
That said, please DON'T store data in your application as void*. Every time you do, you can assume that a developer will probably see it later and die a bit inside (and that developer may even be you).
Edit:
For the calculation in the function, I need to access the elements of the vector, vector>>> accel; Is there a better way????
Consider this:
class FourDimensionalVector
{
public:
FourDimensionalVector(std::size_t x, std::size_t y, std::size_t z, std::size_t a)
: d1{x}, d2{y}, d3{z}, d4{a}
, data{ d1 * d2 * d3 * d4 }
{
}
// ALL access to the vector elements can/should be done through this
double& operator()(std::size_t x, std::size_t y, std::size_t z, std::size_t a)
{
assert(d1 > x); // same for the other dimensions
return data[x * d1 + y * d2 + z * d3 + a];
}
// implement other interface elements here (iteration access, reading the size,
// resetting the values, etc.
private:
std::size_t d1, d2, d3, d4;
std::vector<double> data; // store flattened data
};
With this, you just pass a reference to the data as a parameter, and edit it as needed. I'm not sure the indexing logic is OK (and the code is incomplete), but the idea is the same.
Client code:
void YourFunction(FourDimensionalVector& fdv)
{
fdv[2, 3, 4, 0] = fdv[2, 3, 4, 0] + 0.38;
}
This solution is strongly typed, efficient and clean (it avoids the casting completely and the void*).
[edit by Jojia]: But what is the difference between your solution and this one
vector<vector<vector<vector<double>>>> Xaccel;
void initialize(&Xaccel); //This function initializes values for all elements of Xaccel
void myfunction(&Xaccel){
double x = Xaccel[0][0][0][0];
}
I don't see any difference to your solution. BUT: I thought passing these large objects to the function myfunction might be a problem. Is that correct?
[edit by utnapistim]:
From a semantical point of view, they are the same: both send the same data to the function and allocate the same memory (my solution also stores dimensions, but whatever).
From a maintenance/reusability/testability/modularity point of view, they are quite different: my solution abstracts away the fact that your four dimensional matrix is a vector (of vector of vector ... ).
This will allow you to write your client code using a 4d matrix interface (which you can implement to match the needs of your client code) instead of a vector, and allow you to define your operations in terms of a matrix.
For example, the vector<vector<vector<double>>> code can be used to create a sparse/asymetrical matrix (with the first line in the outer vector being three times longer than the second line and so on). The matrix class would prevent that by cutting access to the vector.
If you choose to keep the vector<vector...> solution, at least typedef it:
typedef vector<vector<vector<double>>> FourDMatrix;
FourDMatrix Xaccel;
void initialize(FourDMatrix& Xaccel);
void myfunction(FourDMatrix& Xaccel);
In this line you create a copy:
vector<double> k = *(static_cast<vector<double>*>(para));
So one of the solution would be to use reference instead:
vector<double> &k = *(static_cast<vector<double>*>(para));
k[0] = 123;
Or pointer
vector<double> *pk = static_cast<vector<double>*>(para);
(*pk)[0] = 123; // but usage is more explicit, so reference would be better
I assume you know what you are doing and you really need to pass vector through a void *. One of the reasons could be passing pointer to vector as a cookie to C API that expects void *. Note there is very low chance to have a good reason to use void * in c++ itself, you should use boost::any or boost::variant for type safety.

passing a multidimensional array to a function with argument of type double *

I am using a library that takes a pointer to a double array as an argument, called as follows:
const int N=10;
const int J=20;
double my_array[N*J];
libFunc(my_array, N, J);
I would prefer to work with multidimensional arrays in my code, and I have discovered that I can call libFunc by dereferencing my multidimensional double array as follows
double my_array2[N][J];
libFunc(&my_array2[0][0], N, J);
However, I am worried that this code might not be portable, that it may not continue to work as N and M get large, and that there may be other hidden problems.
Why is this bad, what should I look out for here? What is the proper way to use multidimensional arrays and pass them to libFunc as if they were ordinary double arrays?
Edit: Read the comments below the selected answer for a discussion of the issue at hand. It seems that if I declare a static array, as is done above, then this code should work on most compilers. However if the array is dynamically allocated there may be an issue.
There is no simple way short of making a copy. Accessing an array outside its bounds is undefined behaviour, and you won't get around this.
Now, it is possible in many situations that your code works, simply because the memory for T[M * N] and for T[M][N] is laid out in the same way. As long as the caller and the callee aren't visible to the compiler at the same time, I would hazard a guess that this should work.
But imagine this scenario:
T a[M][N];
for (size_t i = 0; i != M * N; ++i)
{
a[0][i] = 0;
}
Here the compiler may reason that a[0][N] is out of bounds, and thus there is undefined behaviour, and the compiler may legally omit the entire loop, or make the application crash or wipe your hard disk.
So... take your pick. Undefined behaviour is around somewhere, but you might get lucky.
You are basically screwed: The function expects a double *, so you should give it a double *.
The easiest and safer way to do that would be to use a wrapper. Something like:
template<size_t M, size_t N>
class Double2DArray
{
std::vector<double> m_container ; // It could be a double[M * N]
// But it could explode your stack
// So let's use the heap
public :
// Etc.
Double2DArray()
: m_container(M * N)
{
// I assume the default you want is a vector already
// filled with 0.0
}
size_t getSizeM() { return M ; }
size_t getSizeN() { return N ; }
double & operator() (size_t p_m, size_t p_n)
{
return m_container[p_m * N + p_n] ;
}
double * data()
{
return &(m_container[0]) ;
}
// etc.
} ;
Of course, this code is not complete: At the very least, you should add the const versions of the accessors, probably handle copy-construction and assignment, etc.. I don't know your exact needs, so, your mileage may vary, but the core idea is there...
You could use this wrapper as follow:
void foo()
{
Double2DArray<42, 13> my2DArray ;
// etc.
my2DArray(3, 5) = 3.1415 ; // set some value
double d = my2DArray(13, 2) ; // get some value
// etc.
libFunc(my2DArray.data(), my2DArray.getSizeM(), my2DArray.getSizeN());
}
I would even overload libFunc to be safer:
template<size_t M, size_t N>
inline void libFunc(Double2DArray<M, N> & p_my2DArray)
{
libFunc(p_my2DArray.data(), M, N);
}
This way I could be able to call it without needed to give it again and again the size of the array (it's so easy to mix M and N):
void foo()
{
Double2DArray<42, 13> my2DArray ;
// etc.
libFunc(my2DArray);
}
This is how I would use multidimensional arrays and feed it to a C-like API expected a contiguous array of doubles.
P.S.: If M and N are not know at compile time, you only need to remove the template, and make the M and N parameters of the constructor, and everything works (almost) the same.
C++ uses row-major ordering so your multidimensional array is in fact a continuous 1-dimensional region in memory.
Even if declared for example 2-dimensional, it's accessed via index = x + y * height, so there should be no portability concerns...
The C++ documentation tells:
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
(Here's also an explaination for visual c++)

C++ -- fastest way to set an array of floats to the same value

I naively thought I could use memset for this, but apparently memset is only for chars. Is there a memset-type thing that will work on an array of floats? Or is simple iteration the fastest way to copy a single value to every spot in an array?
I won't speak to what code runs the fastest. You should compare them yourself, in the intended environment.
But, here are two readable, maintainable, correct solutions.
std::fill(std::begin(array), std::end(array), 3.14);
Or, if you have a dynamic array:
std::fill(array, array+size, 3.14);
The standard way is:
std::fill(float_array, float_array + array_size, 0.0f);
I suspect you could beat this standard facility without non-standard methods.
I would recommend std::fill.
std::fill(
floatArray + 0, floatArray + LENGTH
, newValue
);
The compiler knows what you are trying to do here, and decent implementations can provide appropiate optimizations.
You just have to loop through and set all the values to 0 (which is what memset does anyway):
// this is a generic function to set all the elements of an array of any type (that has a public assignment operator and copy constructor) to a specific value
template<typename T>
void setarray(T* array, const T val, unsigned int array_size) {
for (unsigned int i = 0; i < array_size; ++i)
array[i] = val;
}
float* floatArray = new float[x];
setarray(floatArray, 0, x);
If the particular value you want to set all the elements of the array to happens to be 0 and you are using a compile time array, you can use an initialiser list:
float myArray[CONSTANT] = {}; // all values are 0
There is memfill function
http://manpages.ubuntu.com/manpages/natty/man3/memfill.3pub.html
in publib nonstandard library. It can be used in ubuntu.
#include <publib.h>
float a=4.4;
memfill(buf_ptr, buf_size, &a, sizeof(float) );
You can use threads and initialize sub-ranges with separate threads.
If you have a multiple dimensional float array then you could do something like this:
float myMultiDimensionFloats[5][250];
float myfloats[250];
for(int i=0; i<250;i++)
myfloats[i] = 3.14f; // the value you want to store in each float entry
for(int ii=0; ii<5; ii++)
CopyMemory(myMultiDimensionFloats[ii], &myfloats, sizeof(myMultiDimensionFloats[ii]));
Or if you want to use the template from "Seth Carnegie" :
#define LEN(arr) ((int) (sizeof (arr) / sizeof (arr)[0]))
float myMultiDimensionFloats[5][250];
setarray(myMultiDimensionFloats[0], 3.14f, LEN(myMultiDimensionFloats) * LEN(myMultiDimensionFloats[0]));

Passing array with unknown size to function

Let's say I have a function called MyFunction(int myArray[][]) that does some array manipulations.
If I write the parameter list like that, the compiler will complain that it needs to know the size of the array at compile time. Is there a way to rewrite the parameter list so that I can pass an array with any size to the function?
My array's size is defined by two static const ints in a class, but the compiler won't accept something like MyFunction(int myArray[Board::ROWS][Board::COLS]).
What if I could convert the array to a vector and then pass the vector to MyFunction? Is there a one-line conversion that I can use or do I have to do the conversion manually?
In C++ language, multidimensional array declarations must always include all sizes except possibly the first one. So, what you are trying to do is not possible. You cannot declare a parameter of built-in multidimensional array type without explicitly specifying the sizes.
If you need to pass a run-time sized multidimensional array to a function, you can forget about using built-in multidimensional array type. One possible workaround here is to use a "simulated" multidimensional array (1D array of pointers to other 1D arrays; or a plain 1D array that simulates multidimensional array through index recalculation).
In C++ use std::vector to model arrays unless you have a specific reason for using an array.
Example of a 3x2 vector filled with 0's called "myArray" being initialized:
vector< vector<int> > myArray(3, vector<int>(2,0));
Passing this construct around is trivial, and you don't need to screw around with passing length (because it keeps track):
void myFunction(vector< vector<int> > &myArray) {
for(size_t x = 0;x < myArray.length();++x){
for(size_t y = 0;y < myArray[x].length();++y){
cout << myArray[x][y] << " ";
}
cout << endl;
}
}
Alternatively you can iterate over it with iterators:
void myFunction(vector< vector<int> > &myArray) {
for(vector< vector<int> >::iterator x = myArray.begin();x != myArray.end();++x){
for(vector<int>::iterator y = x->begin();y != x->end();++y){
cout << *y << " ";
}
cout << endl;
}
}
In C++0x you can use the auto keyword to clean up the vector iterator solution:
void myFunction(vector< vector<int> > &myArray) {
for(auto x = myArray.begin();x != myArray.end();++x){
for(auto y = x->begin();y != x->end();++y){
cout << *y << " ";
}
cout << endl;
}
}
And in c++0x for_each becomes viable with lambdas
void myFunction(vector< vector<int> > &myArray) {
for_each(myArray.begin(), myArray.end(), [](const vector<int> &x){
for_each(x->begin(), x->end(), [](int value){
cout << value << " ";
});
cout << endl;
});
}
Or a range based for loop in c++0x:
void myFunction(vector< vector<int> > &myArray) {
for(auto x : myArray){
for(auto y : *x){
cout << *y << " ";
}
cout << endl;
}
}
*I am not near a compiler right now and have not tested these, please feel free to correct my examples.
If you know the size of the array at compile time you can do the following (assuming the size is [x][10]):
MyFunction(int myArray[][10])
If you need to pass in a variable length array (dynamically allocated or possibly just a function which needs to take different sizes of arrays) then you need to deal with pointers.
And as the comments to this answer state:
boost::multiarray may be appropriate since it more efficiently models a multidimensional array. A vector of vectors can have performance implications in critical path code, but in typical cases you will probably not notice an issue.
Pass it as a pointer, and take the dimension(s) as an argument.
void foo(int *array, int width, int height) {
// initialize xPos and yPos
assert(xPos >= 0 && xPos < width);
assert(yPos >= 0 && yPos < height);
int value = array[yPos * width + xPos];
}
This is assuming you have a simple two-dimensional array, like int x[50][50].
There are already a set of answers with the most of the common suggestions: using std::vector, implementing a matrix class, providing the size of the array in the function argument... I am only going to add yet another solution based on native arrays --note that if possible you should use a higher level abstraction.
At any rate:
template <std::size_t rows, std::size_t cols>
void function( int (&array)[rows][cols] )
{
// ...
}
This solution uses a reference to the array (note the & and the set of parenthesis around array) instead of using the pass-by-value syntax. This forces the compiler not to decay the array into a pointer. Then the two sizes (which could have been provided as compile time constants can be defined as template arguments and the compiler will deduct the sizes for you.
NOTE: You mention in the question that the sizes are actually static constants you should be able to use them in the function signature if you provide the value in the class declaration:
struct test {
static const int rows = 25;
static const int cols = 80;
};
void function( int *array[80], int rows ) {
// ...
}
Notice that in the signature I prefer to change the double dimension array for a pointer to an array. The reason is that this is what the compiler interprets either way, and this way it is clear that there is no guarantee that the caller of the function will pass an array of exactly 25 lines (the compiler will not enforce it), and it is thus apparent the need for the second integer argument where the caller passes the number of rows.
You can't pass an arbitrary size like that; the compiler doesn't know how to generate the pointer arithmetic. You could do something like:
MyFunction(int myArray[][N])
or you could do:
MyFunction(int *p, int M, int N)
but you'll have to take the address of the first element when you call it (i.e. MyFunction(&arr[0][0], M, N).
You can get round all of these problems in C++ by using a container class; std::vector would be a good place to start.
The compiler is complaining because it needs to know the size of the all but the first dimension to be able to address an element in the array. For instance, in the following code:
int array[M][N];
// ...
array[i][j] = 0;
To address the element, the compiler generates something like the following:
*(array+(i*N+j)) = 0;
Therefore, you need to re-write your signature like this:
MyFunction(int array[][N])
in which case you will be stuck with a fixed dimension, or go with a more general solution such as a (custom) dynamic 2D array class or a vector<vector<int> >.
Use a vector<vector<int> > (this would be cheating if underlying storage was not guaranteed to be contiguous).
Use a pointer to element-of-array (int*) and a size (M*N) parameter. Here be dragons.
First, lets see why compiler is complaining.
If an array is defined as int arr[ ROWS ][ COLS ]; then any array notation arr[ i ][ j ] can be translated to pointer notation as
*( arr + i * COLS + j )
Observe that the expression requires only COLS, it does not require ROWS. So, the array definition can be written equivalently as
int arr [][ COLS ];
But, missing the second dimension is not acceptable. For little more details, read here.
Now, on your question:
Is there a way to rewrite the
parameter list so that I can pass an
array with any size to the function?
Yes, perhaps you can use a pointer, e.g. MyFunction( int * arr );. But, think about it, how would MyFunction() know where to stop accessing the array? To solve that you would need another parameter for the length of the array, e.g. MyFunction( int * arr, size_t arrSize );
Yes: MyFunction(int **myArray);
Careful, though. You'd better know what you're doing. This will only accept an array of int pointers.
Since you're trying to pass an array of arrays, you'll need a constant expression as one of the dimentions:
MyFunction(int myArray[][COLS]);
You'll need to have COLS at compile time.
I suggest using a vector instead.
Pass a pointer and do the indexing yourself or use a Matrix class instead.
yes - just pass it as pointer(s):
MyFunction(int** someArray)
The downside is that you'll probably need to pas the array's lengths as well
Use MyFunction(int *myArray[])
If you use MyFunction(int **myArray) an pass int someArray[X][Y], the program will crash.
EDIT: Don't use the first line, it's explained in comments.
I don't know about C++, but the C99 standard introduced variable length arrays.
So this would work in a compiler that supports C99:
void func(int rows, int cols, double[rows][cols] matrix) {
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
printf("%f", matrix[r][c]);
}
}
}
Note that the size arguments come before the array. Really, only the number of columns has to be known at compile time, so this would be valid as well:
void func(int rows, int cols, double[][cols] matrix)
For three or more dimensions, all but the first dimension must have known sizes. The answer ArunSaha linked to explains why.
Honestly, I don't know whether C++ supports variable-length arrays, so this may or may not work. In either case, you may also consider encapsulating your array in some sort of matrix class.
EDIT: From your edit, it looks like C++ may not support this feature. A matrix class is probably the way to go. (Or std::vector if you don't mind that the memory may not be allocated contiguously.)
Don't pass an array, which is an implementation detail. Pass the Board
MyFunction(Board theBoard)
{
...
}
in reality my array's size is defined by two static const ints in a class, but the compiler won't accept something like MyFunction(int myArray[Board::ROWS][Board::COLS]).
That's strange, it works perfectly fine for me:
struct Board
{
static const int ROWS = 6;
static const int COLS = 7;
};
void MyFunction(int myArray[Board::ROWS][Board::COLS])
{
}
Maybe ROWS and COLS are private? Can you show us some code?
In C++, using the inbuilt array types is instant fail. You could use a boost::/std:: array of arrays or vector of arrays. Primitive arrays are not up to any sort of real use
In C++0x, you can use std::initializer_list<...> to accomplish this:
MyFunction(std::initializer_list<std::initializer_list<int>> myArray);
and use it (I presume) like this (with the range based for syntax):
for (const std::initializer_list<int> &subArray: myArray)
{
for (int value: subArray)
{
// fun with value!
}
}